Write a post
Published Jan 12, 2017Last updated Jan 18, 2017

How to Easily Decipher a Complex Pointer Declarations

How to Easily Decipher a Complex Pointer Declarations

If you have a problem with deciphering complex pointer declarations like int (*(*foo[10])(void))(int) then you are at the right place. In this tutorial, you would learn to decipher any complex pointer declarations.

To decipher complex declarations, remember these two simple rules:

  • Always read declarations from the inside out: Start from the innermost, if any, parenthesis. Locate the identifier that's being declared, and start deciphering the declaration from there.

  • When there is a choice, always favor [] and () over *: If * precedes the identifier and [] follows it, the identifier represents an array, not a pointer. Likewise, if * precedes the identifier and () follows it, the identifier represents a function, not a pointer. (Parentheses can always be used to override the normal priority of [] and () over *.)

This rule actually involves zigzagging from one side of the identifier to the other.

Now deciphering a simple declaration:

    int *a[10];
Applying rule:  


    int *a[10];      "a is"  
         ^  

    int *a[10];      "a is an array"  
          ^^^^ 

    int *a[10];      "a is an array of pointers"
        ^

    int *a[10];      "a is an array of pointers to `int`".  
    ^^^      

Let's decipher the complex declaration like:

    void ( *(*f[]) () ) ();  
by applying the above rules:  

    void ( *(*f[]) () ) ();        "f is"  
              ^  
   
    void ( *(*f[]) () ) ();        "f is an array"  
               ^^ 

    void ( *(*f[]) () ) ();        "f is an array of pointers" 
             ^    

    void ( *(*f[]) () ) ();        "f is an array of pointers to function"   
                   ^^     
 
    void ( *(*f[]) () ) ();        "f is an array of pointers to function returning pointer"
           ^   

    void ( *(*f[]) () ) ();        "f is an array of pointers to function returning pointer to function" 
                        ^^    

    void ( *(*f[]) () ) ();        "f is an array of pointers to function returning pointer to function returning `void`"  
    ^^^^

Here is a GIF demonstrating how you go (click at image for larger view):

pointer declarations

Discover and read more posts from Dan
get started
Enjoy this post?

Leave a like and comment for Dan