How to read any C type definition?

Sometimes when you try to read a C code example or device driver you find a problem in reading some sophisticated  variable definitions like

int (*apa[])[];
int (*apf[])();
int **fpp();
int (*fpa())[];
int (*fpf())();

and there are more complicated definitions.

stress-cartoon

Don’t freak out. The good news is that you don’t have to remember them because there is a simple rule enables you to read any variable definition called “Right-Left” rule.

How does it work?

First of all, you need to know simple symbols that would help you applying the

‘Right-Left’ rule

  • *          as “pointer to”                     – always on the left side
  • [ ]        as “array of”                         – always on the right side
  • ( )        as “function returning”      – always on the right side

Let’s begin with a simple example:

int * x;

 Step 1:

Find the Identifier(The variable name

which in our example is x, so you say ‘x is

Step 2:

Move right until you hit a symbol or run out of symbols or find a closing parenthesis

So you move right and hit the semicolon(there is no more symbols) so you move  left and hit the ‘*’ so you say  ‘x is a pointer to

Step 3:

Keep moving left until you hit a symbol or run out of symbols or find an opening parenthesis

so you hit ‘int’ symbol so ‘x is a pointer to int

Repeat

step 2 and step 3 until you have the full declaration of your variable

More examples:

char * y[];
  1.  Identifier is y so ‘y is
  2. Move right you hit the [] symbol so  ‘y is array of
  3. you ran out of symbols so move left so ‘y is array of pointers to char

char (*z)[];
  1.  Identifier is z so ‘z is’
  2. Move right you hit the closing parenthesis so we move left and hit the ‘*’ symbol so  ‘z is a pointer to
  3. Keep moving left and you hit an opening parenthesis so we move right and hit       ‘[ ]’ symbol so ‘z is a pointer to array of
  4.  Keep moving right and you find that you run out of symbols so move left and you hit the symbol ‘char’ so ‘z is a pointer to array of char

int (*fun)(void);
  1. Identifier is fun so ‘fun is
  2. Move right, you hit the closing parenthesis so you move left and hit the
    ‘*’ symbol so ‘fun is a pointer to
  3. Keep moving left, you hit an opening parenthesis so we move right and hit ‘(void)’  so  ‘fun is a pointer to function takes nothing and returns
  4. Keep moving right, you run out of symbols so move left and hit the symbol ‘int’ so  ‘fun is a pointer to function takes nothing and return int

So it’s simple isn’t it?

Try to decode the declaration of this variable:

char (*(*m)(char *,int))[5][5];

 

References:

Right-Left rule

Advertisements

3 thoughts on “How to read any C type definition?

  1. char (*(*m)(char *,int))[5][5]
    m is a pointer to func
    takes 2 variables
    int & pointer to char
    and returns pointer to 2 dimension array of char

    it’s more simple with the Right-Left rule

    Like

  2. m is a pointer to a function that takes a pointer to character and an int as parameters and returns a pinter of an 5*5 array of chars.
    Thanks. That was really helpful.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s