PeterDonis
Mentor
- 48,834
- 24,959
yungman said:I posted the error message right under the program
Yes, I know. I was trying to get you to actually read it and think about what it is telling you.
yungman said:isn't the two say exact the same thing?
No.
int *ptr = &var;
says "allocate a variable of type pointer to int and store the address of var there".int *pptr = ptr;
says "allocate a variable of type pointer to int and take the pointer that is already stored in the variable ptr and store it there as well".int *pptr; *pptr = ptr;
says "allocate a variable of type pointer to int; at the location the pointer stored in this variable points to, store the pointer that is already stored in the variable ptr".See the difference?
Note that there are actually two things wrong with the last bit of code. Aside from the fact that it is mismatching types (you are trying to store a pointer in a variable of type int, since that is the type of variable that the pointer
pptr
points to), it is trying to store to an undefined location, since *pptr
means "the int variable that pptr points to", but the pointer variable pptr doesn't point to any int variable yet.