Pointers
suggest changeIntroduction
A pointer is a type of variable which can store the address of another object or a function.
Syntax
- <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. */
Remarks
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[2]; /* foo is a array of pointers, can be accessed as *foo[0] and *foo[1] */
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents