Most of the times declarations are simple to read, but it is hard to read some declarations which involve pointer to functions. For example, consider the following declaration from “signal.h”.
Let us see the steps to read complicated declarations.
1) Convert C declaration to postfix format and read from right to left.
2) To convert expression to postfix, start from innermost parenthesis, If innermost parenthesis is not present then start from declarations name and go right first. When first ending parenthesis encounters then go left. Once whole parenthesis is parsed then come out from parenthesis.
3) Continue until complete declaration has been parsed.
Let us start with simple example. Below examples are from “K & R” book.
Let us convert above expression to postfix format. For the above example, there is no innermost parenthesis, that’s why, we will print declaration name i.e. “fp”. Next step is, go to right side of expression, but there is nothing on right side of “fp” to parse, that’s why go to left side. On left side we found “*”, now print “*” and come out of parenthesis. We will get postfix expression as below.
fp * () int
Now read postfix expression from left to right. e.g. fp is pointer to function returning int
Let us see some more examples.
Postfix : daytab *  int
Meaning : daytab is pointer to array of 13 integers.
Postfix : f * (int, int) void
Meaning : f is an array of 10 pointer to function(which takes 2 arguments of type int) returning void
Postfix : x () *  * () char
Meaning : x is a function returning pointer to array of pointers to function returnging char
Postfix : x * () *  char
Meaning : x is an array of 3 pointers to function returning pointer to array of 5 char’s
Postfix : arr * () * () * int
Meaning : arr is an array of 5 pointers to functions returning pointer to function returning pointer to integer
Postfix : bsd_signal(int sig, void(*func)(int)) * (int) void
Meaning : bsd_signal is a function that takes integer & a pointer to a function(that takes integer as argument and returns void) and returns pointer to a function(that take integer as argument and returns void)
This article is compiled by “Narendra Kangralkar“ and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- How does Volatile qualifier of C works in Computing System
- Header files in C/C++ with Examples
- C Programming For Beginners - A 20 Day Curriculum!
- Tr0ll 3 Walkthrough of Check the Flag or CTF Problem
- What are the C programming concepts used as Data Structures
- Array of Structures vs. Array within a Structure in C/C++
- Difference between Identifiers and Variables in C
- Difference between C and Objective C
- Strict Aliasing Rule in C with Examples
- Difference between C and Dart
- Difference between Sentinel and Counter Controlled Loop in C
- Why do we need reference variables if we have pointers
- What are the differences between C and Embedded C?
- Macros and its types in C/C++