Having a hard time telling where an asterisk is related to a pointer.
Here are a few examples of code that are confusing me.
typedef struct X { int i_; double d_; } X;
X x;
X* p = &x;
p->d_ = 3.14159; // dereference and access data member x.d_
(*p).d_ *= -1; // another equivalent notation for accessing x.d_
On line five, we have (*p).d_ *= -1;
. Why are there two asterisks? What does their position mean?
int x = 2;
int* p_x = &x; // put the address of the x variable into the pointer p_x
*p_x = 4; // change the memory at the address in p_x to be 4
assert(x == 4); // check x is now 4
On line 2int* p_x = &x;
, we make a new pointer, but on line 3 *p_x = 4;
, the pointer syntax is still using an asterisk. Why is this the case?
Credit: What does "dereferencing" a pointer mean? for code examples.
Note: Question was "In C pointer syntax, why are asterisks used intermittently?". I changed it to help future visitors.