Computer >> Computer tutorials >  >> Programming >> C programming

Pointer to an Array in C


Pointers are variables which stores the address of another variable. When we allocate memory to a variable, pointer points to the address of the variable. Unary operator ( * ) is used to declare a variable and it returns the address of the allocated memory. Pointers to an array points the address of memory block of an array variable.

The following is the syntax of array pointers.

datatype *variable_name[size];

Here,

datatype − The datatype of variable like int, char, float etc.

variable_name − This is the name of variable given by user.

size − The size of array variable.

The following is an example of array pointers.

Example

#include <stdio.h>
int main () {
   int *arr[3];
   int *a;
   printf( "Value of array pointer variable : %d\n", arr);
   printf( "Value of pointer variable : %d\n", &a);
   return 0;
}

Output

Value of array pointer variable : 1481173888
Value of pointer variable : 1481173880

In the above program, an array pointer *arr and an integer *a are declared.

int *arr[3];
int *a;

The addresses of these pointers are printed as follows −

printf( "Value of array pointer variable : %d\n", arr);
printf( "Value of pointer variable : %d\n", &a);