0

Possible Duplicate:
In C, what is the correct syntax for declaring pointers?
good way to write “pointer to something” in C/C++

I am currently reading a book on C. At the moment I read about pointers.

Basically, I think that I have understood the concept. Anyway one thing puzzles me:

Sometimes the author uses

void *foo;

to create a new pointer, but sometimes it's

void* foo;

Is there a difference? Does it matter? Doesn't it? When to use what? ...?

Community
  • 1
  • 1
Golo Roden
  • 140,679
  • 96
  • 298
  • 425
  • 3
    No technical difference. Doesn't stop people arguing about it for all eternity though. In the end it's just a personal preference. – Flexo Jan 26 '13 at 11:52
  • 1
    Just remember that the `*` binds to the *declarator*, not the type specifier; IOW, `void* foo;` is parsed as `void (*foo);`. – John Bode Jan 26 '13 at 13:12

7 Answers7

3

They are equivalent but the former can be preferable when declaring multiple variables of non-void type in a single line

int *foo, *bar;

clearly declares two pointers, while

int* foo, bar;

declares one pointer and one int

simonc
  • 41,632
  • 12
  • 85
  • 103
3

You can have as many or as few spaces either side of the * as you like. It means exactly the same thing either way:

void*foo;
void *foo;
void* foo;
void * foo;
void                     * foo;
void *                     foo;
void           *           foo;

All are exactly the same thing. If you work on a project, there is usually some "coding style standard" that explains which is preferred in that project. But the compiler won't do any different based on if/where there are spaces.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
2

Both are equivalent and no difference whatsoever.

int *foo, i;

But you should know the difference in declarations like above. Here only foo is of pointer type whereas i is of type int (not int*).

P.P
  • 117,907
  • 20
  • 175
  • 238
1

The two syntaxes are equivalent. The only difference is a matter of style.

md5
  • 23,373
  • 3
  • 44
  • 93
  • Look at the other similar answers. This answer is technically correct, but very terse. If you're going to answer a duplicate at least make it a good answer, not just the bare minimum which overlooks the important point. Your answer says *less* than my comment. – Flexo Jan 26 '13 at 13:51
  • @Flexo: When I've posted this (and when the downote appeared), there were no longer answers. There is no definitive answer to such questions. Therefore I think these "explanations" are sufficient. – md5 Jan 26 '13 at 14:00
1

There is no difference to compiler, as you probably already know.

As of the matter of style, some argue that void* ptr is better because the type name as a whole is isolated. The problem is, that's not how the language works:

int*  ptr, otherptr; /* otherptr is not a pointer here */

Even if you decide against declaring multiple variables at once, the whole idea of having an isolated type name breaks for arrays and function pointers. That's why I prefer the other style:

int *ptr, *otherptr, dontDeclareTooManyThings[N];
Anton Kovalenko
  • 20,999
  • 2
  • 37
  • 69
1

Is there a difference?

Of course there is. (If there wasn't, you wouldn't be asking this.) The star is on one side of the whitespace in the first case, and on the other side in the other.

Does it matter?

It does. Not in terms of program logic, however - the two are parsed exactly the same way. However, it's a matter of personal preference. I generally recommend the second way, i. e. void *ptr, since the pointer qualifier (the asterisk) modifies the variable, and not the type.

When to use what?

Just explained.

  • 1
    The asterisk can be viewed also as the type modifier. Hence you can read `void* ptr` as "type of ptr is void pointer", i.e pointer to anything, later type-cast to the exact type pointer. – Pavel S. Jan 26 '13 at 11:59
0

Both variants are valid. They represent different styles.

consider

char* foo, bar;
char *foo, bar;

in both cases foo is a pointer, bar is not.

also consider (readability)

char* foo() {
    // return something
}
scones
  • 3,317
  • 23
  • 34