Array of Pointers in C

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
By Prateek Narang
Free
star5
Enrolled: 1000
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
Prateek Narang
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

Array and Pointers in C Language hold a very strong relationship. Generally, pointers are the variables which contain the addresses of some other variables and with arrays a pointer stores the starting address of the array. Array name itself acts as a pointer to the first element of the array and also if a pointer variable stores the base address of an array then we can manipulate all the array elements using the pointer variable only. Pointers can be associated with the multidimensional arrays (2-D and 3-D arrays) as well. Also, We can create an array of pointers to store multiple addresses of different variables.

Scope

  • Relationship between pointers and arrays in C.
  • Pointers to 1-D arrays, 2-D arrays and 3-D arrays with explanation and implementation (code).
  • Array of pointers in C with explanation and an example.

Introduction

Pointers and Array representations are very much related to each other and can be interchangeably used in the right context. Arrays can be single or multidimensional and are stored in contiguous memory blocks in our system, so it is easy for pointers to get associated with the arrays.

An array name is generally treated as a pointer to the first element of the array and if we store the base address of the array in another pointer variable, then we can easily manipulate the array using pointer arithmetic in a C Program.

Let us now look at how an array is stored in our system's memory and how to declare and initialize an array then, we will move to the relationship of pointers with arrays.

In C Language, we can declare an integer array using the below statement :

The above statement will allocate 55 integer blocks and will occupy a memory of 20 Bytes in the system (5 * 4 = 20, 5 is size of the array and 44 bytes is the space occupied by an integer block, total = 20).

Below is the representation of how the array is stored in the system's memory. Let the base address allocated by the system to the array is 300.

Array Representation

Note : All the consecutive array elements are at a distance of 4 bytes from each other as an int block occupies 4 bytes of memory in the system (64-bit Architecture). Also, each array element contains a garbage value because we have not initialized the array yet.

Now, let us see the relationship between pointers and arrays.

:::

Relationship between Pointers and Arrays in C

Let an array representation as shown below :

Relationship between Pointers and Arrays in C

With respect to the concept of the pointer, let us see some important points related to arrays in general :

  • 'arr' serves two purposes here, first it is the name of the array and second, arr itself represents the base address of the array i.e. 300300 in the above case, if we print the value in arr then it will print the address of the first element in the array.
  • As the array name arr itself represents the base address of the array, then by default arr acts as a pointer to the first element of the array.
  • arr is the same as &arr and &arr[0] in C Language.
  • If we use dereferencing operator (*) on any of the above representations of array address, we will get the value of the very first element of the array.

Let us look at the program below to see arr, &arr and &arr[0] means the same.

C Program :

Output:

Note : Output address will be different at every run.

You can run and check your code here. (IDE by InterviewBit)

We can see that arr, &arr and &arr[0] are printing the same addresses and values in the output window. So, it is clear from the above program and output that arr, &arr and &arr[0] represent the same address in the system's memory.

Syntax Representing Array in Terms of Pointers in C

In a C Program, we denote array elements as arr[i], where i is the index value. Below is a similar syntax in terms of pointers of how we can represent the array elements using the dereferencing operator (*) on the array name i.e. using the pointers property of the array.

  • * is a dereferencing operator used to extract the value from the address (arr + i).
  • *(arr + i) is the same as arr[i] in a C Program.
  • arr represents the array name and i represents the index value.

Example

Let us look at a program to print the values and address of the array elements using the above syntax.

C Program :

OUTPUT :

Note : Output address will be different at every run.

You can run and check your code here. (IDE by InterviewBit)

Explanation :

  • We have declared and initialized an integer array arr, array representation :

ARR Array Representation

  • (arr + i) represents the address of the value at index i, so *(arr + i) will give the value at ith index (address(arr + i) = address(arr[i])), it is used to print the addresses of the array elements as the value of i changes from 0-4.
  • * is a dereferencing operator used for printing the value at the provided address. *(arr + i) will print the values of the array at consecutive addresses as the value of i changes from 0-4.

Note : From the above example we can conclude that, &arr[0] is equal to arr and arr[0] is equal to *arr. Similarly,

  • &arr[1] is equal to (arr + 1) and arr[1] is equal to *(arr + 1).
  • &arr[2] is equal to (arr + 2) and arr[2] is equal to *(arr + 2) and so on.
  • ...
  • Finally, we can write the above expressions in a fundamental form:
  • &arr[i] is equal to (arr + i) and arr[i] is equal to *(arr + i).

Note : When the array name arr is an operand of sizeof() operator or the & (address-of) unary operator i.e. sizeof(arr) and &arr respectively, then the array name arr refers to the whole array object, thus sizeof(arr) gives us the size of the entire array in bytes and &arr covers the whole array because as we know the array name arr generally means the base address of the array, so arr and &arr are equivalent but arr + 1 and &arr + 1 will not be equal if the array size is more than 1, arr + 1 gives the address of the next element in the array, while &arr + 1 gives the address of the element that is next to the last element of the array (&arr covers the whole array).

Pointer to Array in C

In a pointer to an array, we just have to store the base address of the array in the pointer variable. We know in the arrays that the base address of an array can be represented in three forms, let us see the syntax of how we can store the base address in a pointer variable:

In all the above cases, ptr will store the base address of the array. Now, Let's see an example where we are printing array elements using a pointer to array. We will add consecutive integer values to the pointer ptr using a for loop and with the help of addition arithmetic we are going to print the array elements.

C Program :

OUTPUT :

You can run and check your code here. (IDE by InterviewBit)

Explanation :

  • We have declared and initialized an integer array arr, array representation :

Initializing integer array representation

  • (ptr + i) will give the address of the array elements as the value of i changes from 0-4 as address(ptr + i) = address(arr[i]).
  • * is the dereferencing operator used for printing the value at the provided address. *(ptr + i) will print the values of the array as the value of i changes.

An arithmetic operation on a pointer means that we are modifying the address value of the pointer and not the value pointed by the pointer. Now, let us look at an example of pointer arithmetic with arrays to understand the pointer with array concept deeply.

C Program :

Output :

Note : Output address will be different at every run.

You can run and check your code here. (IDE by InterviewBit)

Explanation:

  • We have declared and initialized an integer array arr of size 5, array representation :

Array Representation of size 5

  • As we know, ptr, &a[0], a and &a are representing the same address, so all representations print the same address value in the output.
  • First for loop (i = 0 to 4) is used to print the address of all the array elements.
  • Second for loop (i = 0 to 4) is used to demonstrate that a[i] = *(a + i) = *(ptr + i) = *(a + i) = a[i]. All these representations of the array elements are equivalent to each other.
  • &arr + 1 gives the address of the element that is next to the last element (&arr + 1 covers the whole array) while a + 1 gives the address of the second element of the array.
  • *(a + 1) prints the value at index 1 in the output and is equivalent to a[1].
  • *a + 1 prints the (value at [index 0]) + 1 and is equivalent to a[0] + 1.
  • (*ptr / 2) prints the (value at [index 0]) / 2, we can't perform division or multiplication operations on pointers directly. (*(p / 2) or *(p * 2) respectively).

Pointer to Multidimensional Arrays in C

Multi-dimensional arrays are defined as an array of arrays. 2-D arrays consist of 1-D arrays, while 3-D arrays consist of 2-D arrays as their elements. Let us see the pointers to 2-D and 3-D arrays in this section to understand the topic better.

Pointer to 2D Arrays

A 2-D array is an array of arrays, we can understand 2-D array as they are made up of n 1-D arrays stored in a linear manner in the memory. 2-D arrays can also be represented in a matrix form.

In the matrix form, there are rows and columns, so let's look at the representation of a 2-D array matrix below where i represents the row number and j represents the column number, arr is the array name.

Pointer to 2D Arrays

Here, array contains 3 1-D arrays as its element, so array name arr acts as a pointer to the 1st 1-D array i.e. arr[0] and not to the first element of the array i.e. arr[0][0]. As we know our system's memory is organized in a sequential manner so it is not possible to store a 2-D array in rows and columns fashion, they are just used for the logical representation of 2-D arrays.

In the above representation, we have combined 3 1-D arrays that are stored in the memory to make a 2-D array, herearr[0],arr[1], arr[2] represents the base address of the respective arrays. So, arr[0], arr[1] and arr[2] act as a pointer to these arrays and we can access the 2-D arrays using the above array pointers.

Array representation in memory

Let us see the syntax of how we can access the 2-D array elements using pointers.

Syntax for representing 2-D array elements :

Note : *(*(arr + i) + j) represents the element of an array arr at the index value of ith row and jth column; it is equivalent to the regular representation of 2-D array elements as arr[i][j].

Let us look at an example, here we are initializing and printing the elements of the 2-D array using the pointers concept.

C Program :

OUTPUT :

Note : Output address will be different at every run.

You can run and check your code here. (IDE by InterviewBit)

Explanation :

  • We have declared and initialized a 2-D array with 9 elements in total. Array representation in the matrix form and array representation in the memory :

Array representation with 9 elements

Array representation in memory 9 elements

  • We have used (*(arr + i) + j) to print the address and *(*(arr + i) + j) to print the value of the array elements in the output.
  • We can see that all the address values are separated by a 4 bytes difference.

Pointer to 3D Arrays in C

When the elements of an array are 2-D arrays, then the array formed is known as 3-Dimensional Array. 3-Dimensional arrays can also be known as array of matrices. Below is a representation of how a 3-D array looks like.

Pointer to 3D Arrays in C

Let us see the syntax of how we can access the 3-D array elements using pointers.

Syntax for Representing 3-D array elements :

Note : *(*(*(arr + i) + j) + k) represents the element of an array arr at the index value of ith row and jth column of the kth array in the array arr; it is equivalent to the regular representation of 3-D array elements as arr[i][j][k].

Let us now look at the example below, we are initializing and printing the elements of the 3-D array using the pointers.

C Program :

Output:

Note: Output address will be different at every run.

You can run and check your code here. (IDE by InterviewBit)

Explanation :

  • We have declared and initialized a 3-D array with 27 elements in total. Array representation in the memory :

3d array representation in memory

  • We have used (*(*(arr + i) + j) + k) to print the address and *(*(*(arr + i) + j) + k) to print the value of the array elements in the output.
  • We can see that all the address values are separated by a 4 bytes difference.

Array of Pointers in C

As we know, arrays are collections of elements stored in contiguous memory locations. An array of pointers is similar to any other array in C Language. It is an array which contains numerous pointer variables and these pointer variables can store address values of some other variables having the same data type.

Syntax to declare a normal array :

Example :

Syntax to declare a pointer array :

Example :

We are using * operator to define that the ptr array is an array of pointers.

An application of an array of pointers is that it becomes easy to store strings in a char pointer array and it also reduces the memory consumption. Let us look at the C Program to understand the array of pointers in a char array.

C Program :

You can run and check your code here. (IDE by InterviewBit)

Output:

Explanation :

  • We have declared and initialized an array of pointers named fruits. It can contain addresses of char type variables only. Array representation and comparison of simple char array with char pointers array in the memory :

Array of pointers representation

  • We are printing the strings pointed by the pointers in the array using the printf() statement.

Conclusion

  • Array name generally acts as a pointer to the array and contains the starting address of the array.
  • Array elements can be accessed and manipulated using a pointer containing the starting address of the array.
  • Syntax for representation of 2-D arrays elements in terms of pointers is *(*(arr + i) + j) (arr[i][j]) and for 3-D arrays elements is *(*(*(arr + i) + j) + k) (arr[i][j][k]).
  • Array of pointers are used to store multiple address values and are very useful in case of storing various string values.