Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct C++ pointer syntax

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?

like image 365
Big Fas Avatar asked Dec 06 '25 10:12

Big Fas


1 Answers

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;
like image 171
Roddy Avatar answered Dec 07 '25 22:12

Roddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!