When declaring pointers in C, there are 3 variants:
Variant A:
int* ptr;
Variant B:
int *ptr;
Variant C:
int * ptr;
- In A, the indirection operator has been appended to the type.
- In B, the indirection operator has been prepended to the variable.
- In C, the indirection operator stands freely in between type and variable.
The way a pointer is declared differs depending on the type of documentation I read. Some authors seem to have a preference for certain variants, others use several.
- Am I correct to assume that there is no difference in functionality between the different variants?
- If yes, is there a convention for which variant one should be using in C?
It matters only when you plan to declare multiple variables of the same type on the same line. For example, if you want multiple int pointers, you need to do this:
Stylistically though, this is confusing when you're only declaring a single variable. Many people like to see the type followed by the variable name, and the type is supposed to be pointer to int, not int, so they prefer:
It's ultimately up to you whether you prefer one form over the other. In 20 years of programming C professionally, I've seen about 50% of people choose one over the other.
is the preferred C style for declaring a pointer to
T
as used in Kernighan & Ritchie's book about C, as well as in ISO/IEC 9899:2018.is the preferred C++ style way to a declare pointer to
T
as used in Stroustrup's book about C++.Both notations are equivalent.
Something nobody else has mentioned is that
corresponds more closely to the language grammar.
int *ptr;
is a declaration, which consists of:int
, followed by*ptr
.(That actually skips a number of steps, but it gets the basic idea across.)
Since declaration follows use, what this means is that
*ptr
is of typeint
. It follows from this thatptr
is of typeint*
.One could argue that this makes it better than
for the same reason that
is better than
Of course you can write
and read it as "
ptr
is of typeint*
". And plenty of programmers do exactly that, and get along just fine (it tends to be the preferred style in C++). The compiler doesn't care which way you do it, and anyone reading your code shouldn't have trouble understanding it either way.But whichever spacing you choose, you need to understand what
int *ptr;
really means, so that when you seein someone else's code (as you inevitably will), you'll immediately understand that
ptr
is a pointer andi
is an int.And if you're working with other programmers on a project, you should follow whatever existing convention is in the coding standards, or if there isn't one, the way the code is already written. I personally prefer
int *ptr;
toint* ptr;
, but using a mixture of both styles is far worse than using either one consistently.In C, whitespace does not matter except where it is needed to separate tokens. Both your variants are syntactically correct.
Variant 1 associates the pointer operator with the type, which is logical enough. For me, that would be enough if there wasn't for the reason why Variant 2 makes sense.
Variant 2 is consistent with the way C declarations are structured. In the C grammar, the pointer operator belongs in the declarator (that is, with the name), not the type. This matters when declaring multiple variables in the same declaration. There are also a number of more esoteric cases where it matters.
Thus, for me, Variant 2 is more consistent with C. But either variant is defensible, and both variants are conventional.
There is absolutely no difference in functionality between
and
Which you use is up to you, there are multiple conflicting coding styles to choose from.
They both mean the same as others have said. There is a trap waiting for you though. Consider this code:
You might think that this declared to pointers to
int
. No so but otherwise. In facta
isint*
butb
isint
. This is one of the reasons for many C programmers preferring to put the*
next to the variable rather than the type. When written like this:you are less likely to be misled as to what
a
andb
are.Having said all that, many coding standards insist that you declare no more than one variable per line, i,e.
If you follow the one variable per line rule then there is definitely no scope for confusion.