Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ehamberg
Created February 21, 2012 11:23
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ehamberg/1875967 to your computer and use it in GitHub Desktop.
Save ehamberg/1875967 to your computer and use it in GitHub Desktop.
C Pointer Declarations

C/C++ Pointer Declaration Syntax – It makes sense!

I never really liked the way pointers are declared in C/C++:

int *a, *b, *c; // a, b and c are pointers to int

The reason is that I am used to reading variable declarations as MyType myVar1, myVar2, myVar3; and I always read “int*” as the type “integer pointer”�. I therefore wanted the following

int* a, b, c; // a is a pointer to int, b and c are ints

to mean that a, b and c all were of type int*, i.e. pointers to int. and I therefore found it slightly annoying to repeat the asterisk for every variable. This also meant that the symbol * had two slightly different meanings to me: (1) It declares a pointer or (2) it dereferences a pointer. I usually don't declare a whole lot of pointers in one line, but still, this is a (minor) annoyance I have briefly discussed with few fellow programmers over the years. Today I started reading C Traps and Pitfalls by Andrew Koenig and after reading one sentence, in chapter two, the pointer declaration syntax suddenly makes – at least some – sense:

[…] Analogously,

float *pf;

means that *pf is a float and therefore that pf is a pointer to a float.

Of course! If we instead of looking at it as a variable a of type int*, read it as *a – i.e. “a dereferenced” –� it makes sense. That is indeed an int, and that also means that * always means “dereference”�.

Discussion

@mightymouse07
Copy link

Whatever gets you through the day. I personally always need the * next to the variable so there's no mistaken what it is. I also, always try to prefix my pointer variables with "p" so that I know what I'm working with later. It's just a matter of style really. I see it both ways in code and always cringe when it's moved away from the variable.

@jwmcglynn
Copy link

My problem with attaching it to the variable is twofold: It blends in with pointer dereferences and it doesn't make sense in the function return value scenario:

int *function_call();

The pointer is not to the function, it is to the return value. My preference is to not take sides, I just put spaces on both sides:

int * function_call();
int * a, * b, * c;

@fryguy1013
Copy link

@jwmcglynn: the function call is the same. If you dererence function_call(), you get an integer.

@mightymouse07
Copy link

@jwmcglynn: ouch... Spaces on both sides? In the case of returning a pointer, I would put the * next to the return type because, clearly, it does not belong next to the function. I've never seen the * next to the function; ever. Spaces on both sides, in my opinion, makes the code less readable. This is all just my opinion. I'm not saying I'm right or an authority on style.

@stylewarning
Copy link

This mode of thought doesn't make a lot of sense (syntactically) with casting.

@paxunix
Copy link

paxunix commented Feb 22, 2012

This is well-defined, you just need to think like a compiler:
http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html

@ifree
Copy link

ifree commented Mar 5, 2012

把优先级记好就可以了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment