Javascript required
Skip to content Skip to sidebar Skip to footer

You Can Assign an Array to a Pointer Variable.

Prerequisite: Pointers Introduction

Pointer to Array

Consider the post-obit program:

C++

#include <iostream>

using namespace std;

int chief()

{

int arr[5] = { i, 2, 3, 4, 5 };

int *ptr = arr;

cout << "\n" << ptr;

return 0;

}

C

#include<stdio.h>

int main()

{

int arr[five] = { 1, two, iii, iv, 5 };

int *ptr = arr;

printf ( "%p\n" , ptr);

return 0;

}

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

          data_type (*var_name)[size_of_array];

Example:

int (*ptr)[10];

Here ptr is pointer that tin point to an array of 10 integers. Since subscript accept higher precedence than indirection, it is necessary to enclose the indirection operator and arrow proper noun inside parentheses. Hither the type of ptr is 'pointer to an array of 10 integers'.
Note : The pointer that points to the 0th chemical element of array and the pointer that points to the whole array are totally dissimilar. The post-obit programme shows this:

C++

#include <iostream>

using namespace std;

int primary()

{

int *p;

int (*ptr)[5];

int arr[5];

p = arr;

ptr = &arr;

cout << "p =" << p << ", ptr = " << ptr<< endl;

p++;

ptr++;

cout << "p =" << p << ", ptr = " << ptr<< endl;

return 0;

}

C

#include<stdio.h>

int main()

{

int *p;

int (*ptr)[5];

int arr[5];

p = arr;

ptr = &arr;

printf ( "p = %p, ptr = %p\n" , p, ptr);

p++;

ptr++;

printf ( "p = %p, ptr = %p\n" , p, ptr);

return 0;

}

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 assortment arr.

  • The base type of p is int while base type of ptr is 'an array of 5 integers'.
  • Nosotros know that the pointer arithmetics is performed relative to the base size, so if we write ptr++, then the arrow ptr volition be shifted forward past twenty bytes.

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

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

C++

#include <$.25/stdc++.h>

using namespace std;

int main()

{

int arr[] = { iii, 5, half-dozen, 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;

}

C

#include<stdio.h>

int primary()

{

int arr[] = { 3, 5, 6, 7, 9 };

int *p = arr;

int (*ptr)[5] = &arr;

printf ( "p = %p, ptr = %p\n" , p, ptr);

printf ( "*p = %d, *ptr = %p\north" , *p, *ptr);

printf ( "sizeof(p) = %lu, sizeof(*p) = %lu\northward" ,

sizeof (p), sizeof (*p));

printf ( "sizeof(ptr) = %lu, sizeof(*ptr) = %lu\n" ,

sizeof (ptr), sizeof (*ptr));

return 0;

}

Output:

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

Pointer to Multidimensional Arrays:

  • Pointers and two dimensional Arrays: In a two dimensional array, we tin access each element by using ii subscripts, where first subscript represents the row number and second subscript represents the cavalcade number. The elements of 2-D array can be accessed with the help of arrow notation also. Suppose arr is a ii-D assortment, we tin access whatever chemical element arr[i][j] of the array using the pointer expression *(*(arr + i) + j). Now we'll encounter how this expression can be derived.
    Let us take a two dimensional assortment arr[3][four]:
int arr[3][4] = { {1, two, three, four}, {five, 6, vii, 8}, {9, 10, 11, 12} };

Since retentiveness in a computer is organized linearly it is not possible to store the two-D assortment in rows and columns. The concept of rows and columns is only theoretical, really, a 2-D assortment is stored in row-major guild i.e rows are placed side by side to each other. The following figure shows how the above two-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 drove of 1-dimensional arrays that are placed 1 later on another. In other words, we tin say that two-D dimensional arrays that are placed ane subsequently another. Then hither arr is an array of 3 elements where each element is a ane-D assortment 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 'arrow to an array of 4 integers', according to pointer arithmetic the expression arr + 1 volition represent the accost 5016 and expression arr + 2 will represent address 5032.
So we can say that arr points to the 0th 1-D assortment, arr + i 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 chemical element of arr, on dereferencing it will go ithursday chemical element of arr which is of course a i-D array. Thus the expression *(arr + i) gives us the base address of ith i-D array.
  • We know, the pointer expression *(arr + i) is equivalent to the subscript expression arr[i]. Then *(arr + i) which is same as arr[i] gives us the base of operations address of ith i-D array.
  • To access an private chemical element of our 2-D assortment, we should be able to access any jth element of ith one-D array.
  • Since the base type of *(arr + i) is int and it contains the accost of 0th element of ithursday 1-D assortment, nosotros can get the addresses of subsequent elements in the ith 1-D array by calculation integer values to *(arr + i).
  • For example *(arr + i) + ane volition stand for the accost of ist element of istchemical element of ith 1-D array and *(arr+i)+two volition represent the address of 2nd element of ith ane-D array.
  • Similarly *(arr + i) + j will represent the address of jth element of ith 1-D array. On dereferencing this expression nosotros tin become the jthursday element of the ith i-D assortment.
  • Pointers and Iii Dimensional Arrays
    In a three dimensional array nosotros can admission each chemical element by using three subscripts. Let united states have a 3-D array-
int arr[2][3][ii] = { {{five, 10}, {6, 11}, {7, 12}}, {{20, 30}, {21, 31}, {22, 32}} };

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

Thus the arrow 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 nosotros can say that arr[i] represents the base accost of ith two-D array and arr[i][j] represents the base address of the jthursday 1-D assortment.

C++

#include <iostream>

using namespace std;

int main()

{

int arr[ii][3][2] = {

{

{5, 10},

{6, eleven},

{7, 12},

},

{

{20, xxx},

{21, 31},

{22, 32},

}

};

int i, j, m;

for (i = 0; i < 2; i++)

{

for (j = 0; j < iii; j++)

{

for (k = 0; thou < 2; k++)

cout << *(*(*(arr + i) + j) +k) << "\t" ;

cout << "\n" ;

}

}

return 0;

}

C

#include<stdio.h>

int main()

{

int arr[2][3][ii] = {

{

{v, 10},

{6, 11},

{seven, 12},

},

{

{20, 30},

{21, 31},

{22, 32},

}

};

int i, j, 1000;

for (i = 0; i < ii; i++)

{

for (j = 0; j < iii; j++)

{

for (k = 0; g < 2; thou++)

printf ( "%d\t" , *(*(*(arr + i) + j) +k));

printf ( "\n" );

}

}

return 0;

}

Output:

5    ten     six    11     vii    12     twenty    xxx     21    31     22    32

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

Subscripting Pointer to an Assortment

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

int arr[three][4] = {{10, eleven, 12, thirteen}, {20, 21, 22, 23}, {thirty, 31, 32, 33}}; int (*ptr)[4]; ptr = arr;

Since ptr is a pointer to an array of 4 integers, ptr + i volition point to ithursday row. On dereferencing ptr + i, nosotros get base of operations address of ith row. To access the address of jth chemical element of ith row we tin add j to the pointer expression *(ptr + i). So the arrow 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 arrow expression *(*(ptr + i) + j) is equivalent to subscript expression ptr[i][j]. So if nosotros have a pointer variable containing the base of operations accost of 2-D array, and then nosotros tin access the elements of array by double subscripting that pointer variable.

C++

#include <iostream>

using namespace std;

int primary()

{

int arr[3][4] = {

{10, 11, 12, xiii},

{20, 21, 22, 23},

{30, 31, 32, 33}

};

int (*ptr)[four];

ptr = arr;

cout << ptr<< " " << ptr + ane<< " " << ptr + two << endl;

cout << *ptr<< " " << *(ptr + 1)<< " " << *(ptr + ii)<< endl;

cout << **ptr<< " " << *(*(ptr + 1) + 2)<< " " << *(*(ptr + 2) + 3)<< endl;

cout << ptr[0][0]<< " " << ptr[1][2]<< " " << ptr[2][3]<< endl;

return 0;

}

C

#include<stdio.h>

int main()

{

int arr[3][4] = {

{10, 11, 12, 13},

{20, 21, 22, 23},

{30, 31, 32, 33}

};

int (*ptr)[4];

ptr = arr;

printf ( "%p %p %p\n" , ptr, ptr + i, ptr + two);

printf ( "%p %p %p\north" , *ptr, *(ptr + 1), *(ptr + 2));

printf ( "%d %d %d\due north" , **ptr, *(*(ptr + 1) + ii), *(*(ptr + 2) + 3));

printf ( "%d %d %d\northward" , ptr[0][0], ptr[1][2], ptr[2][3]);

return 0;

}

Output:

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

This commodity is contributed by Anuj Chauhan. If you lot like GeeksforGeeks and would similar to contribute, you can also write an article using write.geeksforgeeks.org or mail service your article to review-team@geeksforgeeks.org. See your article actualization on the GeeksforGeeks main folio and help other Geeks.
Please write comments if y'all discover anything incorrect, or you want to share more information nigh the topic discussed above.,


wallerhoullatc.blogspot.com

Source: https://www.geeksforgeeks.org/pointer-array-array-pointer/