以下代码的输出是什么,我无法理解* x和x之间有什么区别,我认为是,是吗?
int a = 5 ;
int * xxx ;
xxx = a;
printf("\n\n%d" , *xxx);
printf("\n\n%d" , xxx);
*xxx = a;
printf("\n\n%d" , *xxx);
printf("\n\n%d" , xxx);
答案 0 :(得分:2)
您没有将a
的地址分配给指针xxx
,而是传递其值。此代码将调用未定义的行为。你会得到任何东西。
改变
xxx = a;
到
x = &a;
现在x
是指向a
的指针,当一元运算符*
与指针一起使用时,它会检索存储在地址x
处的值。这称为解除引用。
还要使用%p
说明符打印地址。
printf("\n\n%p" , (void *)x);
答案 1 :(得分:0)
*x
表示存储在x. x
所占据的地址的引用值表示您引用x.
所持有的地址
来到你的代码,
int a = 5 ; // a contains value 5
int * xxx ;// xxx is a integer pointer
xxx = a; // you are assigning 5 to xxx. Leads to undefined behaviour
printf("\n\n%d" , *xxx); //here *x means *xxx? You are printing value stored at address 5 Which leads to undefined behaviour.
printf("\n\n%d" , xxx); // It will print value 5
*xxx = a; // Here you are assigning 5 to address held by xxx which is 5. Undefined behaviour
printf("\n\n%d" , *xxx); //Printing value stored at the address held by xxx,Leads to undefined behaviour
printf("\n\n%d" , xxx); // printing address held by xxx