Array of Pointers in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

In C programming, arrays and pointers simplifying data handling. Arrays names act as pointers to their starting points. Pointers help access data quickly. This collaboration allows for efficient manipulation of data within arrays. This concept also extends to multidimensional arrays and creating arrays of pointers to manage various data. Understanding this interplay equips programmers with a valuable tool for precise data control in C programming.

An Array Of Pointers In C

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.

What Is An Array?

An array in C is a collection of variables, all of the same type, accessed using a common name. The individual elements in an array can be referenced by indexing them with a number, starting from zero.

Here, numbers is an array of 5 integers.

What Is A Pointer?

A pointer in C is a variable that stores the address of another variable. Pointers are powerful tools that allow direct memory access and manipulation.

Here, p is a pointer pointing to the memory location of var

What Is An Array Of Pointers In C?

Pointers and Array representations are very much related to each other and can be interchangeably used in the right context.

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.

Syntax

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.

Array of Pointers to Character

The major use case of Array of pointers is while storing multiple strings.

Syntax

An array of pointers to characters is declared as:

Here, size defines the number of pointers that the array will hold. Each element of the array is a pointer, specifically pointing to a character or the start of a sequence of characters (i.e., a string).

Example

In the given code, names is an array containing three pointers. Each pointer in the array points to the start of a string. The string "John" is pointed to by names[0], "Jane" by names[1], and "Doe" by names[2]. By indexing into the array, you can access and manipulate individual strings.

Memory Representation of an Array of Strings

  1. Array of Pointers: When you declare an array of pointers to hold strings, you're essentially reserving space in memory for the pointers, not the actual strings. Each element of this array is a pointer, typically of size 4 bytes (for a 32-bit system) or 8 bytes (for a 64-bit system), which stores the address of the location where the actual string begins.

  2. Actual Strings: The strings themselves reside in a different part of the memory. When you initialize the array of pointers with string literals (like "John" or "Jane"), these literals are typically stored in a read-only section of the memory.

Illustration: Imagine the memory as a series of boxes, where each box represents a memory location, and its size denotes the number of bytes it can hold. Memory Representation of an Array of Strings

Here:

arr[0], arr[1], and arr[2] are the pointers in the array of pointers. They each store the address of the first character of the respective strings. The actual strings "John", "Jane", and "Doe" are stored separately in the memory. The '\0' denotes the null-terminator, signifying the end of the string.

Array of Pointers to Different Types

While arrays of pointers to characters are commonly used for strings, the concept of arrays of pointers isn't restricted to characters alone. You can have an array of pointers to various data types, including int, float, structures, etc.

Example:

In this example, we utilize an array of void pointers, allowing it to store the address of any data type. However, during dereferencing, we need to cast the pointer to the appropriate type.

Application of Array of Pointers:

Dynamic Data Structures: Arrays of pointers are fundamental in constructing structures like linked lists, trees, and graphs, where each element might point to another structure or data type.

Function Table: If you have an array of function pointers, it can act as a function table, allowing for dynamic function calls based on indices.

Database Records: When handling heterogeneous database records, an array of pointers can point to different fields of various types, facilitating dynamic and flexible record handling.

Efficient Memory Utilization: Especially useful when dealing with arrays of strings where the lengths of strings vary greatly. Instead of using a 2D array and wasting space, pointers can point to strings of dynamic lengths, ensuring efficient space usage.

Disadvantages of Array of Pointers:

Higher Memory Consumption: While they offer dynamic storage advantages, the pointers themselves consume memory. For instance, on a 64-bit system, every pointer will typically consume 8 bytes.

Complexity: Managing arrays of pointers can increase the complexity of the code, making it more challenging to read and understand, especially for those new to the concept of pointers.

Prone to Bugs: Dealing with pointers introduces risks like:

  • Dereferencing null pointers.
  • Accessing memory locations that aren't part of the allocated memory (out-of-bounds access).
  • Memory leaks if dynamic memory allocation (malloc, calloc, etc.) is used and not properly freed.

Conclusion

  • Arrays in C are collections of variables of the same type, accessed by a common name and indexed from zero.
  • Pointers are variables that store the memory addresses of other variables.
  • Array elements are denoted as arr[i], while pointer-based representation uses *(arr + i) to access elements.
  • Syntax for declaring an array of pointers: data_type *array_name[size].
  • Arrays of pointers are valuable for storing multiple addresses, making them ideal for handling strings and dynamic data structures.