简体   繁体   中英

What is the difference between *a=b and a=&b?

Given:

int **a; // (double pointer)
int *b;  //  (pointer)

Is there any difference between *a=b and a=&b ?

The first, *a = b; copies the value of the variable b to the location a points to.

The second, a = &b copies the address of b to a .

*a = b;

You're assigning the value of b to wherever a is pointing to.

a = &b;

Here you're assigning the address of b to a

*a = b Assigning b to the location in memory where a is pointing at

a = &b Assigning the address of b to the variable a .

& it's the operator that gets the address of a variable

* is the operator that is able to retrieve the value pointed by a pointer, the indirection as you should call this process.

so yes, this 2 statements are different.

I think the question here is what is the difference between the two in practice. This example illustrates this:

int x = 10;
int *y;
int *z;

y = &x;
*z = x;

printf("x: %d, *y: %d, *z: %d\n", x, *y, *z);

x = 20;

printf("x: %d, *y: %d, *z: %d\n", x, *y, *z);

The value pointed to by z does not get updated to the new x value of 20, while the value pointed to by y does.

* a = b:* a是一个指针变量,它存储另一个变量的地址,即b,而a =&b:a只是一个普通变量,它存储b的地址,即使它存储b的地址也不会存储完整的地址。取决于数据类型

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM