A pointer is a type of variable which can store the address of another object or a function.
- <Data type> *<Variable name>;
- int *ptrToInt;
- void *ptrToVoid; /* C89+ */
- struct someStruct *ptrToStruct;
- int **ptrToPtrToInt;
- int arr[length]; int *ptrToFirstElem = arr; /* For <C99 ‘length’ needs to be a compile time constant, for >=C11 it might need to be one. */
- int *arrayOfPtrsToInt[length]; /* For <C99 ‘length’ needs to be a compile time constant, for >=C11 it might need to be one. */
The position of the asterisk does not affect the meaning of the definition:
/* The * operator binds to right and therefore these are all equivalent. */ int *i; int * i; int* i;
However, when defining multiple pointers at once, each requires its own asterisk:
int *i, *j; /* i and j are both pointers */ int* i, j; /* i is a pointer, but j is an int not a pointer variable */
An array of pointers is also possible, where an asterisk is given before the array variable’s name:
int *foo; /* foo is a array of pointers, can be accessed as *foo and *foo */
Found a mistake? Have a question or improvement idea? Let me know.
Table Of Contents