POINTERS AND 2-D ARRAY
In C language there is 1-D, 2-D, and 3-D are available.The c language embodies an unusual but powerful capability ---it can treat parts of arrays as arrays.More specifies,each row of a two -dimensional array can be thought of as a one -dimension array.This is a very important fact if we wish to access array elements of a two -dimension array using pointers.
https://programminghubby.blogspot.com/ |
thus, the declaration,
int s[5][2] ;
can be thought of as setting up an array of 5 elements,each of which is one-dimensional array containing 2 int .We refer to an element of a one-dimensional array using a single subscript.similarly,if we can imagine s to be a one-dimensional array ,then we can refer to its 0th elements as s[0], the next element as s[1] ans so on. More specifically , s[0] gives the address of the 0th one-dimensional array , s[1] gives the address of the first one -dimensional array and so on. This fact can be demonstrated by the following program.
/* Demo 2-D array is an array of arrays */
#include<stdio.h>
int main()
{
int s[4][2] =
{
{ 1234, 56},
{1212 , 33},
{1434, 80},
{1312, 78}
} ;
Here is output.....
Address of 0 th 1-D array =65508
Address of 1 th 1-D array =65516
Address of 2 th 1-D array =65524
Address of 3 th 1-D array =65532
Les's figure out how the program works. The compiler knows that s is an array containing 4 one-dimensional arrays, each containing 2 integers.Each one-dimensional array occupies 4 bytes. These one-dimensional arrays are placed linearly. Hence , each one dimensional array starts 4 bytes further along then the last one ,as can be seen in the memory map of the array shown in the figure.
s[0][0] s[0][1] s[1][0] s[1][1] s[2][0] s[2][1] s[3][0] s[3][1]
1234 | 56 | 1212 | 33 | 1434 | 80 | 1312 | 78 |
65508 65512 65516 65520 65524 65528 65532 65536
We know that the expression s[0] and s[1] would yield the address of the 0th and one-dimensional array respectively. From this figure turn out to be 65508 and 65516.
Now, we have been able to reach each one-dimensional array.What remains is to be able to refer to individual elements of a one-dimensional array. Suppose we want to refer to the elements s[2][1] using pointers. We know that s[2] would give the address 65524, the address of the second one-dimensional array. Obviously would give the address 65528. And the value at this address can be obtained by using the value at address operator, saying *(s[2]+1). But , we have already studied while learning one -dimensional arrays that num[i] is same as *(num+i). Similarly , *(s[2]+1) is same as , *(*(s+2))+1). Thus, all the following expression refer to the same element,
s[2][1]
*(s[2]+1)
*(*(s+2)+1)
using these concepts ,the following program prints out each elements of a two-dimensional array using pointer notation.
*/ pointer notation to access 2-D array elements */
# include<stdio.h>
int main()
{
int s[4][2] =
{
{1234, 56},
{1212 , 33},
{1434, 80},
{1312, 78}
} ;
for (j=0 ; i<=3 ; i++)
{
for (j=0 ; j<=1 ; j++)
printf("%d", *(*(s+i)+j)) ;
printf("\n") ;
}
return 0 ;
}
And here is output.....
1234 56
1212 33
1434 80
1312 78
If we can have array to an integer, a pointer to a float , a pointer to a char ,then can we not have a pointer to an array? we certainly can. The following program shows how to build and use it.
Here p is a pointer to an array of two integers. Note that the parentheses in the declaration of p are necessary. Absent of them would make p and array 2 integer pointers. In the outer for loop , each times we store the address of a new one-dimensional array . Thus first time through this loop, p would contain the address of the 0th 1-D array. This address is then assigned to an integer pointer print. Lastly , in the inner for loop using the pointer pin, we have a printed the individual elements of the 1-D array to which p is pointing. But why should we use a pointer to an array to print elements of a 2-D array to a function.
The way there can be an array of ints or an array of floats, similarly there can be an array of pointers.Since a pointer variable always contains an address, an array of pointers would be nothing but a collection of address. The addresses present in the array of pointers can be addresses of isolated variable or addresses of array elements or any other addresses of any other addresses. All rules that apply to an ordinary array apply to the array of pointers as well .
We aren't going to show a programming example that uses a three-dimensional arrays . This is because ,in practice , one-rarely uses this array. However , an example of initializing a three dimensional array will consolidate understanding of subscript :
A 3-D array can be thought of as an array of arrays. The outer array has three elements, each of which is a 2-D array of four 1-D arrays , each of which contains 2 integers. In other words, a 1-D array of two elements is constructed first . Then four such 1-D arrays are placed one below the other to give a 2-D array containing four rows. Then, three such 2-D arrays are placed one behind the other to yield a 3-D array containing three 2-D arrays . In the array declaration , note how the commas have been given .