[SJF Logo]
Steve Friedl's Weblog

December 27, 2003
Tech Tip: Reading C Type Declarations

Almost all C programmers can decode the declaration int *foo[5];, but as the types get more complicated, it gets much harder to just "wing it" when trying to read a type declaration. Just try to decode this one on sight:

char *(*(**foo[8][])())[];
Back in college I started to write a program to parse a declaration and describe it in English, but during the process the "rules" clicked, so I could read them by sight (and never needed to finish the parser).

Every time I teach a C class I have a lesson on how to read arbitrary C type declarations ("How to impress your friends"), and I've finally written it up in a Tech Tip.

Unixwiz.net Tech Tip: Reading C Type Declarations

Posted by Steve at December 27, 2003 03:17 PM | TrackBack
Comments

Great write-up!

By the way, the tool that you mention - explain a C declaration in 'normal English' - already exists, although it might not have existed at the time you thought about writing one. Take a look at cdecl, available from
ftp://ftp.netsw.org/softeng/lang/c/tools/cdecl/
ftp://metalab.unc.edu/pub/linux/devel/lang/c/

G'luck,
Peter

Posted by: Peter Pentchev on December 30, 2003 02:06 AM
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?