37

Possible Duplicate:
Correct way of declaring pointer variables in C/C++

For some time the following has been annoying me, where should I put the star in my pointer notation.

int *var; // 1

and

int* var; // 2

obviously do the same thing, and both notations are correct, but I find that most literature and code I look at use the 1th notation.

wouldn't it be more 'correct' to use the 2th notation, separating the type and the variable name by a whitespace, rather than mixing the type and variable tokens?

Community
  • 1
  • 1
Martin Kristiansen
  • 9,875
  • 10
  • 51
  • 83
  • 8
    It would be if you view things from a type angle. But C's declaration syntax isn't so. `int* p, q;` declares one pointer and one plain `int`. – Daniel Fischer Dec 15 '12 at 17:00
  • 3
    They're the same, it's a personal choice, and this question is both unanswerable and a duplicate hundreds of times over. – Carl Norum Dec 15 '12 at 17:01
  • 2
    @DanielFischer: I wanna give you the points, so if I wanted to declare two pointers I would have to put `int *p, *q`? – Martin Kristiansen Dec 15 '12 at 17:03
  • 1
    Yup, the idea (I don't like it) is "declaration mimics use". So you need an asterisk for every pointer you declare, and brackets for every array (except if you use typedefs to eliminate the need). – Daniel Fischer Dec 15 '12 at 17:05
  • 1
    You're entering a subject of religious zealotry. I agree with your reasoning though. The counter example usually is something like "int* p, q", where I'd say "Just don't do that then". – Frank Osterfeld Dec 15 '12 at 17:07
  • @FrankOsterfeld: I'm with you on "just don't do that.." it makes it look lie the type system is screwed up.. – Martin Kristiansen Dec 15 '12 at 17:09
  • The purpose of a declaration is to determine the type of an object, so I don't see why put emphasis on the use of it obscuring the real type of the object. An just to say it, the first form becomes inconsistent (or, at least, a little bit ugly) if you use pointers to const objects. – effeffe Dec 15 '12 at 17:41
  • @Daniel Fischer : in my `tourbo c` expression `int* p, q;` both are pointer to integer and in my `gcc` its similar as you commented. – Grijesh Chauhan Dec 15 '12 at 18:14
  • @GrijeshChauhan Then your Turbo C violates the standard. If it's older than from 1990 [let's be generous, 1992], it could be excused, though, since the first standard was ratified only in 1989. – Daniel Fischer Dec 15 '12 at 18:38
  • @Daniel Fischer Its means its not compiler dependent but C standards now.--thanks – Grijesh Chauhan Dec 15 '12 at 18:42
  • These details are not worth a question. – Ramy Al Zuhouri Dec 15 '12 at 20:01
  • I thought of `int *p;` as the `*p` is of type `int`. It's strange though when you coniseder that you can initialize `p` like `int foo=42; int *p = &foo;` Here `int* p = &foo;` makes more sense to me. – Semnodime Aug 22 '19 at 03:53

3 Answers3

27

No. Never. <g>

But consider:

int* var1, var2;

Here, the placement of the * is misleading, because it does not apply to var2, which is an int and not an int*.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • 1
    Does the same go for `&` and `const`? – Martin Kristiansen Dec 15 '12 at 17:05
  • 1
    Yes for `&`, no for `const`. – Pete Becker Dec 15 '12 at 22:52
  • 13
    The language I had come to love is not the clean beauty I thought. – Martin Kristiansen Dec 15 '12 at 23:16
  • 7
    @MartinKristiansen You'll notice that again a dozen times... – glglgl Dec 17 '12 at 09:35
  • 1
    i read this question long ago, and thought about it for a while, but i have come to this conclusion: it MUST be "int* var" not "int *var" because of this basic example: when you comment out unused varaibles think about how you comment them out: you never would do this: void somefunc(int /* *var */) {} right?? you have to do this: void somefunc(int* /* var */) {}... because of that analogy, you would be changing the function signature in the first instance, this is my best argument for placing the star on the type not the variable.I just wish SOME PEOPLE would take a hint to this (kicks QT) – osirisgothra Apr 02 '14 at 15:08
19

The Linux kernel coding style convention is:

int *ptr1 , *ptr2;

So I think you should adopt it as your convention.

When declaring pointer data or a function that returns a pointer type, the preferred use of * is adjacent to the data name or function name and not adjacent to the type name. Examples:

char *linux_banner;
unsigned long long memparse(char *ptr, char **retptr);
char *match_strdup(substring_t *s);
0x90
  • 39,472
  • 36
  • 165
  • 245
10

I believe part of the reason for this notation is so that the usage and declaration of a variable look similar.

int *var;
int x;
x = *var;

You can also think of it as saying that dereferencing var will give you an int.

Peter
  • 1,349
  • 11
  • 21
  • 2
    I think that this is the correct rationale. `*` and `&` are used on identifiers in statements that are not definitions or declarations. `x = *var` means to assign the thing dereferenced from `var` to `x`, and similarly `int *var` means "the thing that `var` dereferences to is of type `int`". – Gauthier Mar 02 '15 at 14:41
  • Alas, when I was first learning C, this similarity and method of thinking got me even *more* confused about pointers. It wasn't until I started thinking of pointers as *types* that I understood them. And, of course, this rationale does not apply at all to references in C++. – jamesdlin Jan 25 '19 at 04:20
  • 2
    I thought of `int *p;` as the `*p` is of type `int`. It's strange though when you coniseder that you can initialize `p` like `int foo=42; int *p = &foo;` Here `int* p = &foo;` makes more sense to me. – Semnodime Aug 22 '19 at 03:53