Get the latest tech news

How to read C type declarations (2003)


This site uses advanced css techniques Even relatively new C programmers have no trouble reading simple C declarations such as int foo[5]; // foo is an array of 5 ints char *foo; // foo is a pointer to char double foo(); // foo is a function returning a double but as the declarations get a bit more involved, it's more difficult to know exactly what you're looking at. char *(*(**foo[][8])())[]; // huh ????? It turns out that the rules for reading an arbitrarily-complex C variable declaration are easily learned by even beginning programmers (though how to actually use the variable so declared may be well out of reach).

Interpreting the derived types is usually the sticking point when reading a complex declaration, but this is resolved with operator precedence in the next section. foo isarray ofarray of 8pointer topointer tofunction returningpointer toarray ofpointer tochar We have no idea how this variable is useful, but at least we can describe the type correctly. int (*(* • ) • ())() The red • indicators show the only two places that could possibly hold the variable name, but the leftmost one is the only one that fits the "inside the grouping parens" rule.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of C type declarations

C type declarations