C Array of Pointers

C Pointers

and similar addresses.

A pointer array stores a set of pointers, and each pointer can point to a different data object.

Pointer arrays are commonly used to handle multiple data objects, such as string arrays or arrays of other complex data structures.

Let's look at an example that uses an array of 3 integers:

Example

#include <stdio.h> const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i; for (i = 0; i < MAX; i++) { printf("Value of var[%d] = %d\n", i, var[i] ); } return 0; }

When the above code is compiled and executed, it produces the following results:

Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200

There may be a situation where we want an array to store pointers to int, char, or other data types.

The following is a declaration of a pointer array pointing to integers:

int *ptr[MAX];

Here,ptrDeclared as an array consisting of MAX integer pointers. Therefore, each element in ptr is a pointer to an int value. The following example uses three integers, which will be stored in an array of pointers, as shown below:

Example

#include <stdio.h> const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i, *ptr[MAX]; for ( i = 0; i < MAX; i++) { ptr[i] = &var[i]; /*Assign the address of an integer*/ } for ( i = 0; i < MAX; i++) { printf("Value of var[%d] = %d\n", i, *ptr[i] ); } return 0; }

When the above code is compiled and executed, it produces the following results:

Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200

You can also use a pointer array pointing to characters to store a list of strings, as follows:

Example

#include <stdio.h> const int MAX = 4; int main () { const char *names[] = { "Zara Ali", "Hina Ali", "Nuha Ali", "Sara Ali", }; int i = 0; for ( i = 0; i < MAX; i++) { printf("Value of names[%d] = %s\n", i, names[i] ); } return 0; }

When the above code is compiled and executed, it produces the following results:

Value of names[0] = Zara Ali
Value of names[1] = Hina Ali
Value of names[2] = Nuha Ali
Value of names[3] = Sara Ali

Let's look at another simple example. We first declare a pointer array ptrArray containing three integer pointers. Then, we point these pointers to different integer variables num1, num2, and num3 respectively. Finally, we use the pointer array to access the values of these integer variables.

Example

#include <stdio.h> int main() { int num1 = 10, num2 = 20, num3 = 30; // Declare an array of integer pointers, containing three pointers int *ptrArray[3]; // Point the pointers to different integer variables ptrArray[0] = &num1; ptrArray[1] = &num2; ptrArray[2] = &num3; // Use the array of pointers to access the values of these integer variables printf("Value at index 0: %d\n", *ptrArray[0]); printf("Value at index 1: %d\n", *ptrArray[1]); printf("Value at index 2: %d\n", *ptrArray[2]); return 0; }

When the above code is compiled and executed, it produces the following results:

Value at index 0: 10
Value at index 1: 20
Value at index 2: 30

Pointer arrays are very useful in C, especially when dealing with data structures with an indeterminate number of elements, such as dynamically allocated string arrays or dynamically created struct arrays.

C Pointers

other extensions