两种说法都做同样的事情

时间:2021-02-09 20:20:17

I am sorry to ask you this but i am a newbie and came across this pointer issue. Can you please explain to me why do both statements end up doing the same thing?

我很抱歉问你这个,但我是一个新手,遇到了这个指针问题。你能否向我解释为什么这两个陈述最终会做同样的事情?

int i=7;

* first way

*第一种方式

int *ptrInt;
ptrInt=&i;

********** second way

**********第二种方式

int *p_anotherWay=i;

2 个解决方案

#1


4  

They don't.

The first statement assigns the value of the memory address of i to ptrInt.
The second statement assigns the value of i itself to ptrInt, not its address.

第一个语句将i的内存地址的值赋给ptrInt。第二个语句将i本身的值赋给ptrInt,而不是它的地址。

Also, the second statement isn't totally well-formed. It compiles on some compilers, and doesn't compile on others. (These 'others' are actually doing it right)

此外,第二个声明并不完全正确。它在一些编译器上编译,而不在其他编译器上编译。 (这些'其他人'实际上做得对)

#2


0  

int i = 7;
int *pI = &i; // pointer to i
int *pSomewhere = (int*)i; // pointer to address 0x7

#1


4  

They don't.

The first statement assigns the value of the memory address of i to ptrInt.
The second statement assigns the value of i itself to ptrInt, not its address.

第一个语句将i的内存地址的值赋给ptrInt。第二个语句将i本身的值赋给ptrInt,而不是它的地址。

Also, the second statement isn't totally well-formed. It compiles on some compilers, and doesn't compile on others. (These 'others' are actually doing it right)

此外,第二个声明并不完全正确。它在一些编译器上编译,而不在其他编译器上编译。 (这些'其他人'实际上做得对)

#2


0  

int i = 7;
int *pI = &i; // pointer to i
int *pSomewhere = (int*)i; // pointer to address 0x7