Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
Syntax
2.
What is an Array?
3.
What is a Pointer?
4.
Steps To Create an Array of Pointers
5.
Example of C++ Array of Pointers
5.1.
Code
6.
Frequently Asked Questions
7.
Key Takeaways
Last Updated: Mar 27, 2024

Array of pointers

Author RAJESH RUNIWAL
4 upvotes
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

Array and pointers are intently related to each other. In C++, the name of an array is taken into consideration as a pointer, i.e., the name of an array includes the address of an element. C++ considers the array name because of the address of the first element. For example, if we create an array of marks that hold the integer type data with length 20, then marks will contain the address of the 1st element(marks[0]). Therefore, we can say that an array containing marks is a pointer holding the address of the first element of an array.

Also see, Literals in C, Fibonacci Series in C++

Syntax

type *name_of_pointer [size_or_array];

In the above syntax, if we need to create an array of pointers, then we ought to define the type of an array pointer; after that, we describe the name of our pointer; however, remember usually define a pointer the usage of the ‘‘*’’ astrict symbol in c++, immediately after this we can define the size of the array which means how many factors it is going to hold.

int *mypointers[10];

What is an Array?

We will use an array to include more than one element in a program with the same data type. Allow us to assume that we’re the usage of a total of five integers in any program. There are two different ways wherein we will allocate memory to all of these integers:

  • We can create five integers differently;
  • We can create an array of all of the different integers.

One of the significant advantages of arrays is that it makes it very easy for a programmer to quickly access all the elements inside the program. All elements can be accessed in a single run of a loop in the program.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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};

2. After this, we ought to create an array of pointers with the intention to store the address of the elements.

Example

int *myptr[1];

3. 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];

4. 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;
    }
}

Output

Demo to show array of pointers! 
Value of array! 
10
20
30
40
50
Value of array of pointers! 
10
20
30
40
50

 

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];
}

Variable 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.

Previous article
What is a pointer to an object in C++?
Next article
NaN in C++
Live masterclass