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
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
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
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
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
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
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
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
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
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
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"
How to interpret complex pointer declarations in c?
Reviewed by Mursal Zheker
on
Minggu, Oktober 06, 2013
Rating:
Tidak ada komentar: