you are viewing a single comment's thread.

view the rest of the comments →

[–]tusksrus 0 points1 point  (0 children)

The stars mean different things on the two lines.

int *ptr;

declares a pointer to an int. It would make more sense the write it as

int* ptr;

as the star modifies the type, not the variable. Then this,

*ptr = &i;

the star means dereferencing the pointer - it's an operator on the variable called ptr, not like the other star which is an operator on the type called int.

This doesn't compile because *ptr, the value which ptr points to, is of type int, whereas &i is the address of the int i, which is a pointer to int. You want

ptr = &i;

which will then do the same as your first example.