Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decipher complex pointer declarations in C?

So I want to give an example:

int *pi; // pi is a pointer that points to an integer
const int *cpi; // cpi is a pointer that points to a constant integer
char *pc; // pc is a pointer to a char 

How can I read these:

char **x; //x is a pointer to a char pointer?
char *y[];
char **z[];

Thanks.

like image 691
Koray Tugay Avatar asked Apr 12 '15 15:04

Koray Tugay


People also ask

What is the pointer declarations used in C explain with example?

A pointer is a variable that stores the address of another variable. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. For example, an integer variable holds (or you can say stores) an integer value, however an integer pointer holds the address of a integer variable.

What are complex pointers?

For pointers with multiple levels of indirection, dereferencing operations can be complicated. The COBOL code to perform such dereferencing operations would require multiple 77-level items with a SET operation for each level of indirection.

What is Declarator in C?

A declarator declares an object, function, or reference. as part of a declaration. A declarator has the following form: Declarator syntax (C only)

What is the declaration of pointer?

A pointer declaration names a pointer variable and specifies the type of the object to which the variable points. A variable declared as a pointer holds a memory address.


1 Answers

cdecl.org is often linked to such questions. No doubt that it make easier to decipher any complex declaration, but at the same time it just provide an abstracted information. Being a C or C++ programmer one should know how to decipher complex declaration manually. Spiral Rule help to some extent but fails in some cases. This answer will help programmers to decipher any complex declaration manually.


Remember these two simple rules:

  1. Always read declaration from the inside out.
  2. When there is a choice, always favor [] and () over *.

The first rule simply states that, locate the variable that is being declared and start deciphering the declaration from it.

For second rule, if * precedes the identifier and [] or () follows it, then the identifier represents an array or function (respectively), not a pointer.

Example 1:

char *y[5]; 
  • Variable/identifier is y.
  • * precedes y and follows [].
  • y must be an array.

Combining above deciphering will result in: y is an array of 5 pointers to char.

Also note that you can always use parentheses to override the normal priority of [] or ().

Example 2:

void (*pf) (int);
  • Variable/identifier is pf.
  • *pf is enclosed in parenthesis, it must be a pointer.
  • () follows *pf, means pf must points to a function.
  • Since () encloses int, function must expects an argument of type int.

So, pf is a pointer to function that expects an int argument and returns nothing.

Now, what would you get after deciphering the following declaration

int *(*a[5])(void);  

?

Answer:

a is an array of pointers to functions that expects no argument and returning pointer to int.


Note: Note that both of

char *y[];
char **z[];  

will cause compilation error if they are not declared as arguments of a function. If they are function's argument then char *y[] is equivalent to char **y and char **z[] is equivalent to char ***z.
If that's not the case, then you need to specify the dimension as I did in my first example.

like image 175
haccks Avatar answered Sep 20 '22 04:09

haccks