Live data from Hacker News

How to interpret complex C/C++ declarations (2004)

codeproject.com

11–14 of 14 posts

Re: How to interpret complex C/C++ declarations (2004)

#11
post #10

Doesn't mention the standard "read it backwards to get the type", and consequently recommends "const int" over the superior "int const". int * const p; Is clearly a pointer to pointer to constant pointer to int. (Hint: read from right to left). This is made easier if you always put the const modifier to the right when it is optional. And just never, ever mix commas with pointer declarations.

Not sure why the comment editor trashed "int * const * * p"

Re: How to interpret complex C/C++ declarations (2004)

#12
post #10

Doesn't mention the standard "read it backwards to get the type", and consequently recommends "const int" over the superior "int const". int * const p; Is clearly a pointer to pointer to constant pointer to int. (Hint: read from right to left). This is made easier if you always put the const modifier to the right when it is optional. And just never, ever mix commas with pointer declarations.

HN ate your \* symbols. Try prefixing the line with 4 spaces to trigger code formatting.

Re: How to interpret complex C/C++ declarations (2004)

#13
post #9

The main things to remember is that declaration mirrors use, and you don't actually directly declare the type of the variable, you declare the type of the expression that the declaration mirrors. // x has type int int x; // the expression *x has type int // -> x is something that, when dereferenced, yields an int // -> x is a pointer to int int *x; // x[n] has type int for some integer n // -> x is something, when ap…

I've never noticed this before. Honestly, this makes things a lot clearer. And while it's still not terribly easy to figure out things like char ((x())[5])() it makes a lot more sense.
Post reply on HN