I am struggling with understanding the correct pointer syntax, various sources seem to be doing it differently.
Which one of these is correct?
int* p = NULL;
int * p = NULL;
int *p = NULL;
and these?
*p = &x;
* p = &x;
Sorry for the beginner question but I can't seem to find a straight answer.
Does the * goes right after the data type, right before the var name or in between?
All are valid, and have the same meaning, so it's a matter of personal choice.
The one thing to beware of is this:-
int* i,j;
Despite appearances this declares i as a pointer to int, but j is just an int. See this question here for details. If really you want this, use..
int *i;
int j;
Or, if you insist on having them on one line
int j, *i;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With