C Array of 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
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
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
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
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.
other extensions