Skip to main content

How to interpret complex pointer declarations in c?

The following is the order of precedence, operators and its associativity.

   Precedence Order                   Operators                      Associativity     
1 ()   []  ->  Left To Right
2   ++  --  *  &  Identifier   Right to Left


We have a package named cdecl(compose C type declarations) available in linux which can be used to interpret complex pointer declarations.

How to install cdecl package?
 jp@jp-VirtualBox:~/$ sudo apt-get install cdecl 

The explain statement in cdecl decodes any given C declarations.  Below are the complex C pointer declarations.

int  *num
char  **str
int ( *arr ) [ 15 ]
int  *arr [ 15 ]
int *fun ( )
int ( *fun ) ( int, int )
int ( *arr[] ) ( )
char ( *( *fun ( ) ) [ ] ) ( )
char ( *( *arr[ 5 ] ) ( ) )[ 7 ]

Let us decode the above pointer declarations one by one.

1. int *num;
  jp@jp-VirtualBox:~/$ cdecl
  Type `help' or `?' for help
  cdecl> explain int *num
  declare num as pointer to int
  • Identifier "num" has higher priority (right to left associativity) than Indirection operator(*).  So, the above declaration is read as "num is pointer to integer"

2. char  **str
  cdecl> explain char **str
  declare str as pointer to pointer to char
  • Identifier "str" has higher priority than indirection operators(**).  So, the above above declaration is read as "str is a pointer to pointer to char"

3. int ( *arr ) [ 15 ]
  cdecl> explain int ( *arr ) [ 15 ]
  declare arr as pointer to array 15 of int
  • ( *arr ) has higher priority than [15].  So, first we need to decode ( *arr ).  ( *arr ) can be read as "arr is pointer to"
  • [15 ] can be read as "array 15"
  • Let us concatenate the above decoded information altogether and the resultant would be "arr is a pointer to array 15 of type integer"
4. int  *arr [ 15 ]
  cdecl> explain int  *arr [ 15 ]
  declare arr as array 15 of pointer to int
  • [15] has higher priority than *arr.  [15] can be read as "array 15 of"
  • int *  - pointer to integer
  • Let us concatenate the above decoded information altogether and the resultant would be "arr is array 15 of pointer to integer"
5. int *fun ( )
  cdecl> explain int * fun()
  declare fun as function returning pointer to int
  • fun() has higher priority than (int *).  So, the above declaration can be read as "fun is a function returning pointer to integer"
6. int ( *fun ) (int, int)
  cdecl> explain int ( *fun ) (int, int)
  declare fun as pointer to function(int, int) returning int
  • ( *fun ) has higher priority than (int, int) - (left to right associativity).  So, ( *fun ) can be read as "pointer to function(int, int)"
  • integer is the return value
  • Let us concatenate the above decoded information altogether and the resultant would be "fun is a pointer to function(int, int) returning integer"
7. int ( *arr[] ) ( )
  cdecl> explain int ( *arr[] ) ( )
  declare arr as array of pointer to function returning int
  • ( *arr[] ) has higher priority than () - (left to right associativity).
  • ( *arr[]) - [] has higher priority than indirection operator.  So, ( *arr[] ) can be read as "array of pointer"
  • () - to function
  • integer is the return type
  • Let us concatenate the above decoded information altogether and the resultant would be "arr is an array of pointer to function returning integer"
8. char ( *( *fun ( ) ) [ ] ) ( )
  cdecl> explain char ( *( *fun ( ) ) [ ] ) ( )
  declare fun as function returning pointer to array of pointer to function returning char
  • char ( *( *fun ( ) ) [ ] ) ( ) => ( * fun() ) has the highest priority and it would be interpreted as "function returning pointer"
  • char ( * ( * fun () ) [ ] ) ( ) => [ ] has higher priority than indirection operator(*).  So, square brace is read as "array of"  and the indirection operator is read as "pointer to"
  • character is the return type
  • Let us concatenate the above decoded information altogether and the resultant would be "fun is a function returning pointer to array of pointer to function returning char"
9. char ( *( *arr[ 5 ] ) ( ) )[ 7 ]
  cdecl> explain char ( *( *arr[ 5 ] ) ( ) )[ 7 ]
  declare arr as array 5 of pointer to function returning pointer to array 7 of char

  • char ( *( *arr[ 5 ] ) ( ) )[ 7 ] => ( *arr[ 5 ] ) has the highest priority and it would be interpreted as "array 5 of pointer to function"
  • char ( *( *arr[ 5 ] ) ( ) )[ 7 ] => The highlighted part would be interpreted as "returning pointer to array 7 of character"
  • Let us concatenate the above decoded information altogether and the resultant would be "arr is an array 5 of pointer to function returning pointer to array 7 of char"

Comments

Popular posts from this blog

Array of pointer to structure

Array of pointer to structure is nothing but an array whose elements are pointers to structure How to declare array of pointer to structure? Below is an example declaration for array of pointer to structure. struct student *arr[10]; Here, arr is an array 10 of pointer to structure student. Dynamic memory allocation: Below is the procedure to perform dynamic memory allocation for the structure pointers in an array. struct student { int age; char name[32]; }; fun() { int i = 0; struct student *arr[10]; for (i = 0; i < 10; i++) { /* dynamic memory allocation for 5 structure objects */ arr[i] = (struct student *)malloc(sizeof(struct student) * 5); } } Write a c program to pass an array of structure pointers to another function   #include <stdio.h>   #include <stdlib.h>   #include <string.h>   struct student {         int age, rollno;         char name[32];   };   /* printing details of stude...

Nested function in C

What is nested function? If a function is defined inside another function, then it is called as nested function.  Nested function example in C: Consider the following example, int add(int a, int b) {        void print(int res) {          printf("Result is %d, res);       }       print(a+b);       return 0; } Here, print() is a nested function.  Because, it is defined inside another function named add() . Example C program using nested functions: #include <stdio.h> void add(int a, int b) { void print(int res) { // nested function printf("Result is %d\n", res); return; } print(a + b); // passing sum of a and b as parameter return; } int main() { add(10, 20); return 0; }   Output:   jp@jp-VirtualBox:~/$ ./a.out   Result is 30 Previous Next

Difference between asterisk and ampersand operators in c

Address Operator (& - Ampersand): Returns address of the given variable.  Consider the following example, int *ptr, var = 10; ptr = &var; ptr = &var sets the address of the variable var to pointer ptr.  & is also called as reference operator. Let us try to understand the purpose of reference operator using the following example program.   #include <stdio.h>   int main() {         int var = 10, *ptr;         /* assigning address of variable var to pointer ptr */         ptr = &var;         /* printing the address of the variable var */         printf("Address of var is 0x%x\n", &var);         /* printing the value of pointer ptr */         printf("Value of ptr is 0x%x\n", ptr);         return 0;   }   Output:   jp@jp-VirtualBox:~/$ ./a.out   Address of var is 0x...