*x = i和x =&i之间有什么区别?

rit*_*att 0 c pointers

*x=i和之间有什么区别x=&i

码:

int i=2;
int *x;

*x=i; //what is the difference between this...
x=&i; //...and this??

//Also, what happens when I do these? Not really important but curious.
x=i;
*x=*i;
Run Code Online (Sandbox Code Playgroud)

das*_*ght 5

*x=i; //what is the difference between this...

这会将值赋给i存储在指向的地址的整数x

x=&i; //...and this??

这个分配的地址ix.

请注意,在您的示例x中未分配,因此行为*x=i未定义.

这是一个更好的例子:

int i = 2, j = 5;
printf("%d %d\n", i, j); // Prints 2 5
int *x = &j;
*x = i;
printf("%d %d\n", i, j); // Prints 2 2
Run Code Online (Sandbox Code Playgroud)