对于*p,[p = &a]和[*p = a]的区别详解
(1) p=&a就是用a的地址对p赋值,&p不改变,变的是p
(2) *p=a就是把p所指向的那一内存空间的值赋值为a,&p和p都不改变,变的是p所指向的那一内存空间的值
#include <stdio.h> // 对于*p,[p = &a]和[*p = a]的区别详解
int main() {
int init = ;
// p=&a就是用a的地址对p赋值,&p不改变,变的是p
int a = ;
int *p = &init;
printf("[p] = %p; [*p] = %d; [&p] = %p;\n", p, *p, &p); //[p] = 0x7ffe12baad9c; [*p] = 0; [&p] = 0x7ffe12baad90;
p = &a;
printf("[p] = %p; [*p] = %d; [&p] = %p;\n", p, *p, &p); //[p] = 0x7ffe12baad98; [*p] = 10; [&p] = 0x7ffe12baad90;
printf("[init] = %d;\n", &init, init); //0 // *q=b就是把q所指向的那一内存空间的值赋值为b,&q q都不改变,变的是q所指向的那一内存空间的值,即init的值
int b = ;
int *q = &init;
printf("[&b] = %p; [q] = %p; [*q] = %d; [&q] = %p;\n", &b, q, *q, &q); //[q] = 0x7ffe12baad9c; [*q] = 0; [&q] = 0x7ffe12baad80;
*q = b;
printf("[&b] = %p; [q] = %p; [*q] = %d; [&q] = %p;\n", &b, q, *q, &q); //[q] = 0x7ffe12baad9c; [*q] = 20; [&q] = 0x7ffe12baad80;
printf("[&init] = %p; [init] = %d;\n", &init, init); //
return ;
}
代码分析: