Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . Pointer Vs. Array: Find the Difference Between Pointer to an Array and Array of Pointers

Pointer Vs. Array: Find the Difference Between Pointer to an Array and Array of Pointers

 


What is a Pointer to an Array?

The array pointer is an alternative name to a pointer to an array. We generally make use of this pointer for accessing the various components of any given array. The pointer ptr basically focuses on the 0th component of any given array. Likewise, one can easily declare a point that is capable of pointing to a whole array- rather than only a single array component.

What is an Array of Pointers?

It refers to an array of various pointer variables, also called the pointer arrays. One can easily create separate pointer variables pointing towards different ranges of values. And we can also create a single integer array of pointers pointing towards all the available values.

Difference Between a Pointer to an Array and Array of Pointers

Here is a list of the differences present between Pointer to an Array and Array of Pointers.

ParametersPointer to an ArrayArray of Pointers
Uses and PurposesA user creates a pointer for storing the address of any given array.A user creates an array of pointers that basically acts as an array of multiple pointer variables.
Alternative NamesIt is alternatively known as an array pointer.These are alternatively known as pointer arrays.
AllocationOne can allocate these during the run time.One can allocate these during the compile time.
Initialization at DefinitionYou cannot initialize a pointer to the definition.You can easily initialize an array at the definition level.
NatureIt is dynamic in nature.It is static in nature.
ResizingOne can easily resize the allocated memory of a pointer later at any given time.Once we declare the size of an array, we cannot resize it any time we want according to our requirements.
Type of StorageA typical pointer variable is capable of storing only a single variable within.The size of any given array decides the total number of variables that it can store within.

Pointer to an array: Pointer to an array is also known as array pointer. We are using the pointer to access the components of the array.

  int a[3] = {3, 4, 5 }; 
  int *ptr = a; 

We have a pointer ptr that focuses to the 0th component of the array. We can likewise declare a pointer that can point to whole array rather than just a single component of the array. Syntax:

data type (*var name)[size of array];

Declaration of the pointer to an array:

// pointer to an array of five numbers
 int (* ptr)[5] = NULL;     

The above declaration is the pointer to an array of five integers. We use parenthesis to pronounce pointer to an array. Since subscript has higher priority than indirection, it is crucial to encase the indirection operator and pointer name inside brackets. Example: 

// C++ program to demonstrate
// pointer to an array.
 
#include <iostream>
using namespace std;
 
int main()
{
 
    // Pointer to an array of five numbers
    int(*a)[5];
 
    int b[5] = { 1, 2, 3, 4, 5 };
 
    int i = 0;
 
    // Points to the whole array b
 
    a = &b;
 
    for (i = 0; i < 5; i++)
          cout << *(*a + i) << endl;
 
    return 0;
}
 
// This code is contributed by sarajadhav12052009
Output:
1
2
3
4
5

Array of pointers: “Array of pointers” is an array of the pointer variables. It is also known as pointer arrays. Syntax:

int *var_name[array_size];

Declaration of an array of pointers:

 int *ptr[3];

We can make separate pointer variables which can point to the different values or we can make one integer array of pointers that can point to all the values. Example: 

// C++ program to demonstrate
// example of array of pointers.
 
#include <iostream>
using namespace std;
 
const int SIZE = 3;
 
int main()
{
 
    // creating an array
    int arr[] = { 1, 2, 3 };
 
    // we can make an integer pointer array to
    // storing the address of array elements
    int i, *ptr[SIZE];
 
    for (i = 0; i < SIZE; i++) {
 
        // assigning the address of integer.
        ptr[i] = &arr[i];
    }
 
    // printing values using pointer
    for (i = 0; i < SIZE; i++) {
 
        cout << "Value of arr[" << i << "] = " << *ptr[i] << endl;
    }
}
 
// This code is contributed by sarajadhav12052009
Output:
Value of arr[0] = 1
Value of arr[1] = 2
Value of arr[2] = 3

Example: We can likewise make an array of pointers to the character to store a list of strings. 

#include <stdio.h>
 
const int size = 4;
 
void main()
{
 
    // array of pointers to a character
    // to store a list of strings
    char* names[] = {
        "amit",
        "amar",
        "ankit",
        "akhil"
    };
 
    int i = 0;
 
    for (i = 0; i < size; i++) {
        printf("%s\n", names[i]);
    }
}
Output:
amit
amar
ankit
akhil

Pointer to Array

Consider the following program: 

#include <iostream>
using namespace std;
 
int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
int *ptr = arr;
 
cout <<"\n"<< ptr;
return 0;
}
 
// thus code is contributed by shivanisinghss2110

In this program, we have a pointer ptr that points to the 0th element of the array. Similarly, we can also declare a pointer that can point to whole array instead of only one element of the array. This pointer is useful when talking about multidimensional arrays. 
Syntax:  

data_type (*var_name)[size_of_array];

Example: 

int (*ptr)[10];

Here ptr is pointer that can point to an array of 10 integers. Since subscript have higher precedence than indirection, it is necessary to enclose the indirection operator and pointer name inside parentheses. Here the type of ptr is ‘pointer to an array of 10 integers’. 
Note : The pointer that points to the 0th element of array and the pointer that points to the whole array are totally different. The following program shows this: 
 

// C++ program to understand difference between
// pointer to an integer and pointer to an
// array of integers.
#include <iostream>
using namespace std;
int main()
{
    // Pointer to an integer
    int *p;
     
    // Pointer to an array of 5 integers
    int (*ptr)[5];
    int arr[5];
     
    // Points to 0th element of the arr.
    p = arr;
     
    // Points to the whole array arr.
    ptr = &arr;
     
    cout << "p =" << p <<", ptr = "<< ptr<< endl;
    p++;
    ptr++;
    cout << "p =" << p <<", ptr = "<< ptr<< endl;
     
    return 0;
}
 
// This code is contributed by SHUBHAMSINGH10

Output: 

p = 0x7fff4f32fd50, ptr = 0x7fff4f32fd50
p = 0x7fff4f32fd54, ptr = 0x7fff4f32fd64

p: is pointer to 0th element of the array arr, while ptr is a pointer that points to the whole array arr
 

  • The base type of p is int while base type of ptr is ‘an array of 5 integers’.
  • We know that the pointer arithmetic is performed relative to the base size, so if we write ptr++, then the pointer ptr will be shifted forward by 20 bytes.

The following figure shows the pointer p and ptr. Darker arrow denotes pointer to an array. 
 

On dereferencing a pointer expression we get a value pointed to by that pointer expression. Pointer to an array points to an array, so on dereferencing it, we should get the array, and the name of array denotes the base address. So whenever a pointer to an array is dereferenced, we get the base address of the array to which it points. 
 

// C++ program to illustrate sizes of
// pointer of array
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int arr[] = { 3, 5, 6, 7, 9 };
    int *p = arr;
    int (*ptr)[5] = &arr;
     
    cout << "p = "<< p <<", ptr = " << ptr << endl;
    cout << "*p = "<< *p <<", *ptr = " << *ptr << endl;
     
    cout << "sizeof(p) = "<< sizeof(p) <<
            ", sizeof(*p) = " << sizeof(*p) << endl;
    cout << "sizeof(ptr) = "<< sizeof(ptr) <<
        ", sizeof(*ptr) = " << sizeof(*ptr) << endl;
    return 0;
}
 
// This code is contributed by shubhamsingh10

Output: 

p = 0x7ffde1ee5010, ptr = 0x7ffde1ee5010
*p = 3, *ptr = 0x7ffde1ee5010
sizeof(p) = 8, sizeof(*p) = 4
sizeof(ptr) = 8, sizeof(*ptr) = 20

Pointer to Multidimensional Arrays:

  • Pointers and two dimensional Arrays: In a two dimensional array, we can access each element by using two subscripts, where first subscript represents the row number and second subscript represents the column number. The elements of 2-D array can be accessed with the help of pointer notation also. Suppose arr is a 2-D array, we can access any element arr[i][j] of the array using the pointer expression *(*(arr + i) + j). Now we’ll see how this expression can be derived. 
    Let us take a two dimensional array arr[3][4]
     
int arr[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };

Since memory in a computer is organized linearly it is not possible to store the 2-D array in rows and columns. The concept of rows and columns is only theoretical, actually, a 2-D array is stored in row-major order i.e rows are placed next to each other. The following figure shows how the above 2-D array will be stored in memory.
 

Each row can be considered as a 1-D array, so a two-dimensional array can be considered as a collection of one-dimensional arrays that are placed one after another. In other words, we can say that 2-D dimensional arrays that are placed one after another. So here arr is an array of 3 elements where each element is a 1-D array of 4 integers. 
We know that the name of an array is a constant pointer that points to 0th 1-D array and contains address 5000. Since arr is a ‘pointer to an array of 4 integers’, according to pointer arithmetic the expression arr + 1 will represent the address 5016 and expression arr + 2 will represent address 5032. 
So we can say that arr points to the 0th 1-D array, arr + 1 points to the 1st 1-D array and arr + 2 points to the 2nd 1-D array. 
 

In general we can write: 

arr + i  Points to ith element of arr ->
Points to ith 1-D array

 

  • Since arr + i points to ith element of arr, on dereferencing it will get ith element of arr which is of course a 1-D array. Thus the expression *(arr + i) gives us the base address of ith 1-D array.
  • We know, the pointer expression *(arr + i) is equivalent to the subscript expression arr[i]. So *(arr + i) which is same as arr[i] gives us the base address of ith 1-D array.
  • To access an individual element of our 2-D array, we should be able to access any jth element of ith 1-D array.
  • Since the base type of *(arr + i) is int and it contains the address of 0th element of ith 1-D array, we can get the addresses of subsequent elements in the ith 1-D array by adding integer values to *(arr + i).
  • For example *(arr + i) + 1 will represent the address of 1st element of 1stelement of ith 1-D array and *(arr+i)+2 will represent the address of 2nd element of ith 1-D array.
  • Similarly *(arr + i) + j will represent the address of jth element of ith 1-D array. On dereferencing this expression we can get the jth element of the ith 1-D array.
  • Pointers and Three Dimensional Arrays 
    In a three dimensional array we can access each element by using three subscripts. Let us take a 3-D array- 
     
int arr[2][3][2] = { {{5, 10}, {6, 11}, {7, 12}}, {{20, 30}, {21, 31}, {22, 32}} };

We can consider a three dimensional array to be an array of 2-D array i.e each element of a 3-D array is considered to be a 2-D array. The 3-D array arr can be considered as an array consisting of two elements where each element is a 2-D array. The name of the array arr is a pointer to the 0th 2-D array. 
 

Thus the pointer expression *(*(*(arr + i ) + j ) + k) is equivalent to the subscript expression arr[i][j][k]. 
We know the expression *(arr + i) is equivalent to arr[i] and the expression *(*(arr + i) + j) is equivalent arr[i][j]. So we can say that arr[i] represents the base address of ith 2-D array and arr[i][j] represents the base address of the jth 1-D array. 

// C++ program to print the elements of 3-D
// array using pointer notation
#include <iostream>
using namespace std;
int main()
{
  int arr[2][3][2] = {
                       {
                         {5, 10},
                         {6, 11},
                         {7, 12},
                       },
                       {
                         {20, 30},
                         {21, 31},
                         {22, 32},
                       }
                     };
  int i, j, k;
  for (i = 0; i < 2; i++)
  {
    for (j = 0; j < 3; j++)
    {
       for (k = 0; k < 2; k++)
          cout << *(*(*(arr + i) + j) +k) << "\t";
       cout <<"\n";
    }
  }
 
  return 0;
}
 
// this code is contributed by shivanisinghss2110

Output: 

5    10    
6    11    
7    12    
20    30    
21    31    
22    32

The following figure shows how the 3-D array used in the above program is stored in memory. 
 

Subscripting Pointer to an Array

Suppose arr is a 2-D array with 3 rows and 4 columns and ptr is a pointer to an array of 4 integers, and ptr contains the base address of array arr

int arr[3][4] = {{10, 11, 12, 13}, {20, 21, 22, 23}, {30, 31, 32, 33}};
int (*ptr)[4];
ptr = arr;

 

Since ptr is a pointer to an array of 4 integers, ptr + i will point to ith row. On dereferencing ptr + i, we get base address of ith row. To access the address of jth element of ith row we can add j to the pointer expression *(ptr + i). So the pointer expression *(ptr + i) + j gives the address of jth element of ith row and the pointer expression *(*(ptr + i)+j) gives the value of the jth element of ith row. 
We know that the pointer expression *(*(ptr + i) + j) is equivalent to subscript expression ptr[i][j]. So if we have a pointer variable containing the base address of 2-D array, then we can access the elements of array by double subscripting that pointer variable. 
 

// C++ program to print elements of a 2-D array
// by scripting a pointer to an array
#include <iostream>
using namespace std;
 
int main()
{
  int arr[3][4] = {
                    {10, 11, 12, 13},
                    {20, 21, 22, 23},
                    {30, 31, 32, 33}
                  };
  int (*ptr)[4];
  ptr = arr;
  cout << ptr<< " "<< ptr + 1<< " "<< ptr + 2 << endl;
  cout << *ptr<< " "<< *(ptr + 1)<< " "<< *(ptr + 2)<< endl;
  cout << **ptr<< " "<< *(*(ptr + 1) + 2)<< " "<< *(*(ptr + 2) + 3)<< endl;
  cout << ptr[0][0]<< " "<< ptr[1][2]<< " "<< ptr[2][3]<< endl;
  return 0;
}
 
// This code is contributed by shivanisinghss2110

Output:  

0x7ffead967560 0x7ffead967570 0x7ffead967580
0x7ffead967560 0x7ffead967570 0x7ffead967580
10 22 33
10 22 33

Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.