C Language Pointers

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

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] */


Got any C Language Question?