Is there a convention for pointer declarations in

2020-05-23 01:28发布

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?

8条回答
Fickle 薄情
2楼-- · 2020-05-23 01:34

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:

int *a, *b, *c;

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:

int* a;
int* b;
int* c;

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.

查看更多
3楼-- · 2020-05-23 01:35
T *a;

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.

T* a;

is the preferred C++ style way to a declare pointer to T as used in Stroustrup's book about C++.

Both notations are equivalent.

查看更多
祖国的老花朵
4楼-- · 2020-05-23 01:37

Something nobody else has mentioned is that

int *ptr;

corresponds more closely to the language grammar.

  • int *ptr; is a declaration, which consists of:
    • a declaration-specifier int, followed by
    • a declarator, *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 type int. It follows from this that ptr is of type int*.

One could argue that this makes it better than

int* ptr;

for the same reason that

x = y+z;

is better than

x=y + z;

Of course you can write

int* ptr;

and read it as "ptr is of type int*". 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 see

int *ptr, i;

in someone else's code (as you inevitably will), you'll immediately understand that ptr is a pointer and i 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; to int* ptr;, but using a mixture of both styles is far worse than using either one consistently.

查看更多
Luminary・发光体
5楼-- · 2020-05-23 01:42

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.

查看更多
Luminary・发光体
6楼-- · 2020-05-23 01:54

There is absolutely no difference in functionality between

int* ptr;

and

int *ptr;

Which you use is up to you, there are multiple conflicting coding styles to choose from.

查看更多
smile是对你的礼貌
7楼-- · 2020-05-23 01:54

They both mean the same as others have said. There is a trap waiting for you though. Consider this code:

int* a, b;

You might think that this declared to pointers to int. No so but otherwise. In fact a is int* but b is int. 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:

int *a, b;

you are less likely to be misled as to what a and b are.

Having said all that, many coding standards insist that you declare no more than one variable per line, i,e.

int* a;
int b;

If you follow the one variable per line rule then there is definitely no scope for confusion.

查看更多
登录 后发表回答