C++ Array of Pointers
Before we explain the concept of pointer arrays, let's first look at an example that uses an array composed 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 or char or other data types. Below is a declaration of an array of pointers to integers:
int *ptr[MAX];
Here,ptrIt is declared 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 an array of pointers 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 Aliother extensions