How to use a pointer with array in C

C Pointer & Array

In C programming, a pointer is a variable that stores the memory address of another variable. It is used to get the memory address of a variable as well as the value at the memory location.

In C programming, an array is a collection of elements of the same data type stored in contiguous memory locations.

A pointer can be used to point to the elements of an array.

Various operations on arrays can be performed using pointers.

Accessing Array Elements Using a Pointer

A pointer can be used to traverse the array or access its elements.

The name of the array itself acts as a pointer to the first element of the array.

The following example demonstrates how to access array elements using a pointer:

Example of accessing array elements using a pointer
Output of the above example
The first element of the array: 55
The second element of the array: 12
The fourth element of the array: 25

Explanation of code:

  • int arr[] = {55, 12, 15, 25, 30};: This line declares an array of integer with 5 elements.
  • int *ptr = arr;: This line declares a pointer variable named 'ptr' (an asterisk '*' is used before the variable name) and initializes the variable with the array name 'arr'. Here, the name of the array (arr) points to the first element of the array.
  • printf("The first element of the array: %d\n", *ptr);: This line prints the first element of the array using pointer 'ptr' variable.
  • ptr++; The pointer ptr is incremented. Since 'ptr' is an integer, incrementing it by 1 moves the pointer to the next element in the array. Now, incremented 'ptr' points to the second element of the array.
  • printf("The second element of the array: %d", *ptr);: This line prints the second element of the array using pointer 'ptr' variable.

Modifying Array Elements Using a Pointer

A pointer can be used to modify the array elements as demonstrated in the following example:

Example of modifying array elements using a pointer
Output of the above example
The third element of the array: 15
The modified third element of the array: 65

Explanation of code:

  • int arr[] = {55, 12, 15, 25, 30}; : This line declares an array of integer with 5 elements.
  • int *ptr = arr;: This line declares a pointer variable named 'ptr' (an asterisk '*' is used before the variable name) and initializes the variable with the array name 'arr'. The name of the array (arr) points to the first element of the array.
  • printf("The third element of the array: %d\n", arr[2]); : This line prints the third element of the array.
  • *(ptr + 2) = 65; : The line modifies the third element of the array by assigning new value '65'. Here the pointer 'ptr' is incremented by 2 to point the third element of the array. Now, the incremented 'ptr' points to the third element of the array.
  • printf("The modified third element of the array: %d", arr[2]); : This line prints the modified third element of the array.

Passing an Array to a Function Using a Pointer

An array can be passed to a function by passing a pointer to its first element as demonstrated in the following example.

Example of passing an array to a function using a pointer
Output of the above example
55
12
15
25
30

Explanation of code:

  • void printArray(int *ptr, int size): This line defines a function name 'printArray' with pointer parameter '*ptr' and another parameter 'size'.
  • for (int i = 0; i < size; i++): This is a for loop inside the function.
  • printf("%d\n", *(ptr + i));: This prints the value the pointer points to. The pointer is incremented by adding loop variable 'i'.
  • int numbers[] = {55, 12, 15, 25, 30};: This line initializes an array of integer named 'numbers' with 5 elements.
  • int size = sizeof(numbers) / sizeof(numbers[0]);: This find the length of the 'numbers' array.
  • printArray(numbers, size);: This calls the function 'printArray'. Here the array 'numbers' is passed to the function. When an array is passed to a function, what actually gets passed is a pointer to the first element of the array.

Live Code Playground

In the following C code editor, you can practice by creating more pointers with arrays. Click the 'Execute Code' button to execute the code; the executed output will be displayed in the following C Code Output frame. Practice until you become comfortable and proficient with your code.

C logo C Online Editor
C Code Output