| Asterisk ('*')

Previous | Punctuators | Next

The asterisk (`'*'`) in a variable expression creates a pointer to a type. For example,

    int a, *b;

In this example `'a'` is an integer, but `'b'` is a pointer to an integer.
Punctuators asterisk, brackets and parentheses may be mixed together to create very complex data types (which are sometimes very hard to understand). When mixed together, parentheses have the greatest precedence, then brackets, and finally, asterisks. This will be illustrated with a set of examples, which are given in the following table:

    int x;

A simple integer

    int x[5];

An array (with 5 elements) of integers

    int x[5][6];

An array (with 5 elements) of arrays (with 6 elements) of integers; such "array of arrays" may be interpreted as a 5x6 matrix

    int *x;

A pointer to an integer

    int *x[5];

An array of pointers to integers

    int x();

A function (more precise, a prototype of a function) which returns an integer

    int x(int a);

A function which accepts one integer argument, and which returns an integer

    int x(int a,int *b);

A function which accepts two arguments, the first one is an integer, and second one is a pointer to an integer, and which returns an integer

    int x(int,int*);

The same as above, but actual names of arguments may be omitted in function prototypes (but not in function definitions)

    int *x();

A function which returns a pointer to an integer

    int *x(int);

A function which accepts one integer argument, and which returns a pointer to an integer

    int **x;

A pointer to a pointer to an integer (this declaration is logically equivalent with the next one)

    int *x[];

An array of unknown size of pointers to integers (this declaration is logically equivalent with the previous one)

    int (*x)[5];

A pointer to an array (with 5 elements) of integers

    int (*x)();

A pointer to a function which returns an integer

    int (*x)(int,int);

A pointer to a function which accepts two integer arguments and which returns an integer

    int (*x[5])();

An array of pointers to a function which returns an integer

    int (*x(*x())[5])();

A function which returns a pointer to an array of pointers to a function which returns an integer

    int (*x(*x(int))[5])(int*);

A function which accepts one argument which is a pointer to an integer, and which returns a pointer to an array of pointers to a function which accepts one integer argument and which returns an integer

    int (*(*x[5])())[6];

An array (with 6 elements) of pointers to a function which returns a pointer to an array (with 5 elements) of integers

Confused? Yes, it's C!
If you simply omit the actual variable name, you will get an anonymous type. Such anonymous types may be used with sizeof or typecast operators, or in lists of arguments in function prototypes. For example, `'int*'` is anonymous pointer to an integer, `'int(*)()'` is anonymous pointer to a function which returns an integer, and `'int(*[])(int*)'` is anonymous array (of unknown size) of pointers to a function which returns an integer and which accepts one argument which is a pointer to an integer.
Note: The GNU C extends the usage of asterisk to allow computed goto.