(tco 4) what is the term for the numbers used inside parentheses to reference an array element?

Prerequisite: Pointers Introduction

Arrow to Array

Consider the following program:

C++

#include <iostream>

using namespace std;

int main()

{

int arr[5] = { ane, two, 3, 4, five };

int *ptr = arr;

cout << "\n" << ptr;

return 0;

}

C

#include<stdio.h>

int chief()

{

int arr[5] = { 1, ii, 3, 4, v };

int *ptr = arr;

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

return 0;

}

In this program, we accept a pointer ptr that points to the 0th element of the array. Similarly, we can besides declare a pointer that can betoken to whole array instead of only i element of the array. This pointer is useful when talking well-nigh multidimensional arrays.
Syntax:

          data_type (*var_name)[size_of_array];

Example:

int (*ptr)[10];

Here ptr is pointer that tin can point to an assortment of x integers. Since subscript have higher precedence than indirection, information technology is necessary to enclose the indirection operator and pointer name inside parentheses. Here the blazon of ptr is 'pointer to an assortment 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 dissimilar. The following programme shows this:

C++

#include <iostream>

using namespace std;

int principal()

{

int *p;

int (*ptr)[v];

int arr[five];

p = arr;

ptr = &arr;

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

p++;

ptr++;

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

render 0;

}

C

#include<stdio.h>

int main()

{

int *p;

int (*ptr)[5];

int arr[five];

p = arr;

ptr = &arr;

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

p++;

ptr++;

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

return 0;

}

Output:

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

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

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

The post-obit figure shows the pointer p and ptr. Darker arrow denotes pointer to an array.

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

C++

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

using namespace std;

int chief()

{

int arr[] = { iii, 5, vi, 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, nine };

int *p = arr;

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

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

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

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

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) = 8, sizeof(*p) = 4 sizeof(ptr) = 8, sizeof(*ptr) = xx

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 exist accessed with the help of pointer notation also. Suppose arr is a 2-D array, we tin can access any element arr[i][j] of the array using the arrow expression *(*(arr + i) + j). Now nosotros'll see how this expression can exist derived.
    Let united states have a two dimensional array arr[3][four]:
int arr[iii][4] = { {ane, two, 3, 4}, {5, 6, 7, 8}, {9, 10, xi, 12} };

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

Each row can be considered as a 1-D assortment, so a two-dimensional array tin can be considered as a collection of one-dimensional arrays that are placed one after another. In other words, nosotros can say that two-D dimensional arrays that are placed one afterwards another. So here arr is an array of 3 elements where each element is a 1-D array of four integers.
We know that the proper name of an array is a constant arrow that points to 0th 1-D assortment and contains address 5000. Since arr is a 'pointer to an assortment of 4 integers', co-ordinate to pointer arithmetics the expression arr + 1 will represent the address 5016 and expression arr + two will represent address 5032.
So nosotros can say that arr points to the 0th 1-D assortment, arr + 1 points to the 1st 1-D array and arr + two points to the twond i-D array.

In general we tin write:

          arr + i  Points to ith element of arr ->                                Points to ith 1-D assortment                  
  • Since arr + i points to ithursday chemical element of arr, on dereferencing it volition become ith element of arr which is of course a ane-D array. Thus the expression *(arr + i) gives us the base accost of ith 1-D assortment.
  • We know, the pointer expression *(arr + i) is equivalent to the subscript expression arr[i]. So *(arr + i) which is aforementioned as arr[i] gives usa the base address of ith 1-D array.
  • To admission an individual element of our 2-D array, we should be able to admission any jthursday element of ithursday 1-D array.
  • Since the base type of *(arr + i) is int and information technology contains the address of 0th element of ith ane-D array, nosotros can go the addresses of subsequent elements in the ith 1-D assortment past adding integer values to *(arr + i).
  • For example *(arr + i) + 1 will represent the address of 1st element of 1stchemical element of ith 1-D array and *(arr+i)+2 will represent the address of 2nd element of ith i-D assortment.
  • Similarly *(arr + i) + j will correspond the address of jth chemical element of ith 1-D array. On dereferencing this expression we can get the jth chemical element of the ith 1-D array.
  • Pointers and Three Dimensional Arrays
    In a three dimensional array we can access each element by using iii subscripts. Permit united states of america have a 3-D array-
int arr[2][3][2] = { {{5, 10}, {half dozen, 11}, {seven, 12}}, {{xx, 30}, {21, 31}, {22, 32}} };

We can consider a three dimensional array to exist an array of 2-D array i.east each element of a iii-D assortment is considered to be a two-D array. The 3-D assortment arr can be considered as an array consisting of two elements where each element is a ii-D array. The name of the array arr is a pointer to the 0th two-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]. And then we can say that arr[i] represents the base of operations accost of ith two-D array and arr[i][j] represents the base address of the jth ane-D assortment.

C++

#include <iostream>

using namespace std;

int main()

{

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

{

{5, 10},

{vi, xi},

{7, 12},

},

{

{xx, 30},

{21, 31},

{22, 32},

}

};

int i, j, g;

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

{

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

{

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

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

cout << "\n" ;

}

}

return 0;

}

C

#include<stdio.h>

int main()

{

int arr[2][iii][two] = {

{

{v, 10},

{6, 11},

{7, 12},

},

{

{20, 30},

{21, 31},

{22, 32},

}

};

int i, j, thou;

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

{

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

{

for (grand = 0; thousand < 2; one thousand++)

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

printf ( "\n" );

}

}

render 0;

}

Output:

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

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

Subscripting Arrow to an Array

Suppose arr is a 2-D array with 3 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[3][4] = {{10, eleven, 12, xiii}, {20, 21, 22, 23}, {30, 31, 32, 33}}; int (*ptr)[iv]; ptr = arr;

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

C++

#include <iostream>

using namespace std;

int chief()

{

int arr[3][4] = {

{x, eleven, 12, 13},

{20, 21, 22, 23},

{30, 31, 32, 33}

};

int (*ptr)[iv];

ptr = arr;

cout << ptr<< " " << ptr + 1<< " " << ptr + ii << endl;

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

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

cout << ptr[0][0]<< " " << ptr[i][2]<< " " << ptr[ii][three]<< endl;

return 0;

}

C

#include<stdio.h>

int main()

{

int arr[three][4] = {

{10, 11, 12, 13},

{20, 21, 22, 23},

{thirty, 31, 32, 33}

};

int (*ptr)[four];

ptr = arr;

printf ( "%p %p %p\n" , ptr, ptr + one, ptr + ii);

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

printf ( "%d %d %d\northward" , **ptr, *(*(ptr + i) + two), *(*(ptr + ii) + three));

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

return 0;

}

Output:

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

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can likewise write an commodity using write.geeksforgeeks.org or mail your commodity to review-team@geeksforgeeks.org. See your article actualization on the GeeksforGeeks primary folio and assistance other Geeks.
Delight write comments if yous notice anything incorrect, or you want to share more information virtually the topic discussed in a higher place.,



ricecamraithe.blogspot.com

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

0 Response to "(tco 4) what is the term for the numbers used inside parentheses to reference an array element?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel