-2

Is there any differences between these two declarations?

int* a;
int *a;

Or these two declarations are the same (pointer to an integer)?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Guilherme
  • 443
  • 5
  • 22
  • Well, what did your beginner C book say? –  Nov 30 '13 at 22:04
  • 4
    They are exactly the same declaration as far as the C compiler is concerned. It's a matter of style and personal preference which variant to use. – suspectus Nov 30 '13 at 22:05
  • I'm not exactly a C beginner. I'm asking that because gcc was giving me a warning when I was using the first style with a struct. – Guilherme Dec 01 '13 at 03:50
  • 1
    @Guilherme. Most definitely the issue was somewhere else. Or you ran into the problem stated in my answer. – Guido Dec 01 '13 at 18:54

1 Answers1

7

They're exactly the same, but here's a small gotcha I came across when first learning C years ago. The * binds to the variable, not the type. This means that

int* a, b;

Declares a as a pointer to int, and b as an int. To declare both as pointers, one should do.

int *a, *b;

This is why I prefer to place the * next to the name.

Guido
  • 2,571
  • 25
  • 37
  • 1
    more here ;) http://stackoverflow.com/questions/15212126/difference-between-char-var-and-char-var – Joe DF Nov 30 '13 at 22:13
  • @JoeDF Strangely enough, those are my exact same notation conventions! Thanks! – Guido Nov 30 '13 at 22:15
  • Ok, thanks. I'm asking that because gcc was giving me a warning when I was using "struct foo* pointer" declaration. – Guilherme Dec 01 '13 at 03:48