How to read complex pointers in C Programming?
Assign the priority to the pointer declaration considering precedence and associative according to following table.
| Operator | Precedence | Associative |
| (),[] | 1 | Left to right |
| *, identifier | 2 | Right to left |
| Data type | 3 |
Where:
- (): This operator behaves as function operator.
- []: This operator behaves as array subscription operator.
- : This operator behaves as pointer operator .
- Identifier: it is name of pointer variable.
- Data type: Data types also includes modifier (like signed int, long double etc.)
How to read following pointer?
char (* ptr)[3]
Step 1: () and [] enjoys equal precedence. So rule of associative will decide the priority. Its associative is left to right So first priority goes to ().
Step 2: Inside the bracket * and ptr enjoy equal precedence. From rule of associative (right to left) first priority goes to ptr and second priority goes to *.
Step3: Assign third priority to [].
Step4: Since data type enjoys least priority so assign fourth priority to char.
ptr is pointer to such one dimensional array of size three which content char type data.
How to read following pointer?
float (* ptr)(int)
Assign the priority considering precedence and associative.
Now read it following manner:
ptr is pointer to such function whose parameter is int type data and return type is float type data.
Assign the priority of each function parameter separately and read it also separately.
Understand it through following example.
void (*ptr)(int (*)[2],int (*) void))
Assign the priority considering rule of precedence and associative.
Now read it following manner:
ptr is pointer to such function which first parameter is pointer to one dimensional array of size two which content int type data and second parameter is pointer to such function which parameter is void and return type is int data type and return type is void.
How to read following pointer?
int ( * ( * ptr ) [ 5 ] ) ( )
Assign the priority considering rule of precedence and associative.
Now read it following manner:
ptr is pointer to such array of size five which content are pointer to such function which parameter is void and return type is int type data.
How to read following pointer?
double*(*(*ptr)(int))(double **,char c)
ptr is pointer to function which parameter is int type data and return type is pointer to function which first parameter is pointer to pointer of double data type and second parameter is char type data type and return type is pointer to double data type.
How to read following pointer?
unsigned **(*(*ptr)[8](char const *, …)
Assign the priority considering rule of precedence and associative.
ptr is pointer to array of size eight and content of array is pointer to function which first parameter is pointer to character constant and second parameter is variable number of arguments and return type is pointer to pointer of unsigned int data type.