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.
Example of C++ Array of Pointers
Code
#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.
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.
Key Takeaways
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.