There is a difference to the reader (human), but there's none to the compiler.
int* p- widely used by C++ programmers
int* p, qwrongly implies that bothpandqare pointers (leading to a preference for declaring this on two lines, which also improves readability when there are assignments, and makes it easier to quickly cut/paste or comment specific lines/variables)int* pvisually separates the type from the identifier*punambiguously indicates a dereference (assuming you put spaces around your binaryoperator*ala2 * 3)- in C++
...&xis clearly taking an address while...& xmust be declaring a reference variable, and... & ...is the bitwise operator
int *p- widely used by C programmers
int *p, qclearly reflectspbeing a pointer andqnot being.int *pvisually confuses the type with the identifier- visually indistinguishable from a pointer dereference (for better or worse)
They are the same thing it comes down to preference in the end.
The bonus of the latter is that you can more sensibly define multiple pointers on one line ... ie
int *foo, *bar, *cow;
creates 3 pointers where as the following:
int* foo, bar, cow;
creates 1 pointer and 2 ints. Personally I prefer int* as it shows its a pointer to an integer and I define all my variables on seperate lines. Some will disagree with me, some will agree with me. Do whatever you prefer and, more than anything, BE CONSISTENT!
No comments:
Post a Comment