这个*ptr=len是什么意思 在阅读C++中有关复制构造函数的一些代码时,我发现了一个类似于这个语句的代码:*> *PTR= LeN/COD>(其中代码> Le是一个 int 类型变量, *pTR 是一个 int < /代码>类型指针。p>

这个*ptr=len是什么意思 在阅读C++中有关复制构造函数的一些代码时,我发现了一个类似于这个语句的代码:*> *PTR= LeN/COD>(其中代码> Le是一个 int 类型变量, *pTR 是一个 int < /代码>类型指针。p>,c++,C++,这意味着什么?这意味着ptr变量的内容不是len的值 int foo; int *a = &foo; // a points to the memory of foo. int value = 10; (*a) = value; // changes the value of foo via pointer indirection. 换句话说,ptr指出的变量的值具有len的值 int foo; int *a = &foo; // a points to the memory

这意味着什么?

这意味着ptr变量的内容不是len的值

int foo;
int *a = &foo; // a points to the memory of foo.
int value = 10;
(*a) = value; // changes the value of foo via pointer indirection.
换句话说,ptr指出的变量的值具有len的值

int foo;
int *a = &foo; // a points to the memory of foo.
int value = 10;
(*a) = value; // changes the value of foo via pointer indirection.

现在(*a)等于10

它是分配给
ptr
所指向的值/位置。换句话说,我们将
len
的值赋给
ptr
所指向的值

例如:

// Declare and initialize int variable.
int x = 0;
// Declare pointer-to-int variable, initialize to be pointing at x.
int *xp = &x;

// Assign to the value _pointed to by_ xp, which is x.  In other words,
// assigning to *xp is the same thing as assigning to x.
*xp = 1;

// Will display 1, because x was reassigned through *xp.
std::cout << x << std::endl;
//声明并初始化int变量。
int x=0;
//声明指向int变量的指针,初始化为指向x。
int*xp=&x;
//分配给由xp指向的值_,即x。换句话说,,
//分配给*xp与分配给x是一样的。
*xp=1;
//将显示1,因为x是通过*xp重新分配的。

STD:你应该读一本关于C++的图图/书,尤其是PosisSoice,比如你可以使用AAA.和<代码>删除<代码>:我感觉到使用<代码>新< /COD>和 Auto >这里偏离了这个例子试图说明的,因为OP可能是绝对初学者。我可以采用老式的C++ 03风格,但我觉得现代C++中的解释在很多情况下更容易。现代C++禁止使用<代码>新< /COD>和<代码>删除< /代码>。我认为只使用指向自动存储的指针(即,
inti;int*a=&I;…
)会更容易。这就是“通过”的含义