What is a Pointer?
The pointers in C point towards the variables present in a program. They hold the addresses of the variables. The program will use these addresses to get the right to the variables and modify them. right here are the three crucial things that come into play when dealing with pointers in C:
- Declaration of the pointer variable
- Initialisation of the pointer variable
- Using the pointer to access the variable
Check out this problem - Find Duplicate In Array
Steps To Create an Array of Pointers
We can follow given steps to create an array of pointers:
1. First, we want to create an array that includes a few elements. Let’s say ten elements for now.
Example
int myarray [2] = {10, 20};

You can also try this code with Online C++ Compiler
Run Code2. After this, we ought to create an array of pointers with the intention to store the address of the elements.
Example
int *myptr[1];

You can also try this code with Online C++ Compiler
Run Code3. To get the address of the element, we have got the ‘‘&’’ operator in c++. This can deliver us the address of the element in memory.
Example
myptr[1] = &myarray[1];

You can also try this code with Online C++ Compiler
Run Code4. Then, we can store this address of the elements into the array of pointers by using iterating them using for loop. This depends on the good judgment of ways we need them to be loaded in an array.
5. Now, we will access the array elements using the array of pointers. It will supply us with the same result.
Array of Pointers to Character
An Array of Pointers to Character is a collection where each element is a pointer that stores the address of a character or a string. In C++, this is often used to create arrays of strings. Each pointer in the array can point to different character arrays or string literals. This allows dynamic management of strings, making it flexible for string manipulation.
Example:
char *arr[] = {"Hello", "World", "C++"};
Here, arr is an array of pointers where each pointer points to the first character of a string.
Array of Pointers to Different Types
An Array of Pointers to Different Types is an array where each element is a pointer, and these pointers can point to different data types. While arrays of pointers to a single type are common, handling multiple data types using a void pointer (void*) is possible. However, this requires typecasting when accessing the actual data.
Example:
int a = 10;
float b = 5.5;
void *arr[] = {&a, &b};
In this example, arr is an array of void pointers pointing to an integer and a float.
Example of C++ Array of Pointers
Implementation
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout << "Demo to show array of pointers! \n";
int my_array[5] = {10, 20, 30, 40, 50};
int *myptr[5];
cout << "Value of array! \n";
for (int i = 0; i < 5; i++)
{
cout << my_array[i] << endl;
}
myptr[0] = &my_array[0];
myptr[1] = &my_array[1];
myptr[2] = &my_array[2];
myptr[3] = &my_array[3];
myptr[4] = &my_array[4];
cout << "Value of array of pointers! \n";
for (int i = 0; i < 5; i++)
{
cout << *myptr[i] << endl;
}
}

You can also try this code with Online C++ Compiler
Run CodeOutput
Demo to show array of pointers!
Value of array!
10
20
30
40
50
Value of array of pointers!
10
20
30
40
50

You can also try this code with Online C++ Compiler
Run Code
Try and compile with online c++ compiler.
Application of Array of Pointers
- String Management: Used to manage arrays of strings where each pointer points to the first character of a string, allowing flexible string manipulation.
- Dynamic Memory Allocation: Enables managing dynamically allocated arrays or objects, improving memory efficiency in programs.
- Multidimensional Arrays: Can be used to create jagged arrays where each row has a different number of elements, making it more space-efficient.
- Function Pointers: Allows creating arrays of function pointers for situations like callback functions or event handling.
Advantages of Array of Pointers
- Memory Efficiency: Reduces memory usage by pointing to only the needed data, rather than creating multiple copies of it.
- Dynamic Allocation: Supports dynamic memory management, allowing allocation of memory as needed at runtime.
- Flexibility: Provides flexibility in handling data structures such as jagged arrays or strings of varying lengths.
- Efficient String Manipulation: Allows easy manipulation of strings without copying large amounts of data.
Disadvantages of Array of Pointers
- Complexity: Managing an array of pointers can add complexity to the code, especially when dealing with dynamic memory.
- Risk of Memory Leaks: If memory is not properly deallocated, it can lead to memory leaks and inefficient resource usage.
- Pointer Errors: Mistakes in pointer arithmetic or dereferencing can cause crashes or undefined behavior in the program.
- Harder Debugging: Debugging pointer-related issues, such as dangling pointers or incorrect memory access, can be more challenging.
Frequently Asked Questions
1. What is the output of the C++ program with arrays and pointers?
int main()
{
int size=4;
int a[size];
a[0]=5;a[1]=6;
a[2]=7;a[3]=8;
cout << *(a+2) << “” << a[1];
}

You can also try this code with Online C++ Compiler
Run CodeVariable size is already defined. So a[size] is allowed. *(a+2) == a[2].So, output will be 7 6.
2. What’s the value of an array element that isn’t initialized?
It depends on Storage Class
3. What will be returned if you try to access an Array variable outside its size?
Some garbage value will be returned.
4. Can we change the starting index of an array from 0 to 1 in any way?
No, You can’t change the C++ Basic rules of the Zero Starting Index of an Array.
Conclusion
We learned about the array of pointers by using an array of pointers. We can control our array without problems because they may be very bound in nature. We need to access the elements by using the address of the elements. This address is not anything; the element’s element’s location from the memory to immediately access them. Now, After brushing up on your concepts about this topic, the next thing you should do is to head over to our practice platform Coding Ninjas Studio to practice top problems on every topic. You can also read the interview experiences of top product-based companies.