Array Part -2
Array Part -2
12 4 7 9 10
a[0] a[1] a[2] a[3] a[4]
100
The name a of the array is a constant expression, whose value is the address of the 0th
location.
An array variable is actually just a pointer to the first element in the array.
You can access array elements using array notation or pointers.
a[0] is the same as *a = *(a+0)
a[1] is the same as *(a + 1)
a[2] is the same as *(a + 2)
a = a+0 = &a[0]
a+1 = &a[1]
a+i = &a[i]
&(*(a+i)) = &a[i] = a+i
*(&a[i]) = *(a+i) = a[i]
#include <stdio.h>
int main()
{ int a[3];
for(int i=0;i<3;i++)
{
scanf("%d",a+i); // &a[i]
}
for(int j=0;j<3;j++)
{
printf("%d",*(a+j)); // a[j]
}
return 0;
}
Calculate address of element in the array
1-D array
Consider a single dimensional array as a[lowerbound------------------upperbound]
The base address of array =BA
Size of each element in array =c
Total elements in the array are given by(upperbound-lowerbound+1)
Then address of any random element a[i] is given by:
Location [a[i]]= BA + (i-lowerbound) * c
Example: int a[5]={12,4,7,9,10};
a[0---4]
100
BA= 102 104 106 108
12 4 7 9 10
a[0] a[1] a[2] a[3] a[4]
100
100
BA= 102 104 106 108
12 4 7 9 10
a[3] a[4] a[5] a[6] a[7]
100
Output:
Elements of array are 45 67 34 78 90
In C language, one can have arrays of any dimensions. Arrays can be 1-dimensional, 2-
dimensional, 3-dimensional, etc.
Initialization of 2D Array in C
In the 1D array, we don't need to specify the size of the array if the declaration and
initialization are being done simultaneously. However, this will not work with 2D arrays. We
will have to define at least the second dimension of the array. The two-dimensional array
can be declared and defined in the following way.
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
1. #include<stdio.h>
2. int main(){
3. int i=0,j=0;
4. int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}}; //traversing 2D array
5. for(i=0;i<4;i++){
6. for(j=0;j<3;j++){
7. printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
8. } //end of j
9. } / /end of i
10.return 0;
11.}
Output:
arr[0][0] = 1
arr[0][1] = 2
arr[0][2] = 3
arr[1][0] = 2
arr[1][1] = 3
arr[1][2] = 4
arr[2][0] = 3
arr[2][1] = 4
arr[2][2] = 5
arr[3][0] = 4
arr[3][1] = 5
arr[3][2] = 6
2D array example: Storing elements in a matrix and printing it.
1. #include <stdio.h>
2. void main ()
3. {
4. int arr[3][3],i,j;
5. for (i=0;i<3;i++)
6. {
7. for (j=0;j<3;j++)
8. {
printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
9. }
10. }
11. printf("\n printing the elements ....\n");
12. for(i=0;i<3;i++)
13. {
14. printf("\n");
15. for (j=0;j<3;j++)
16. {
a. printf("%d\t",arr[i][j]);
17. }
18. }
19. }
Output:
Enter a[0][0]: 56
Enter a[0][1]: 10
Enter a[0][2]: 30
Enter a[1][0]: 34
Enter a[1][1]: 21
Enter a[1][2]: 34
Enter a[2][0]: 45
Enter a[2][1]: 56
Enter a[2][2]: 78
56 10 30
34 21 34
45 56 78
2-D array
Consider a two dimensional array as A[lb1------ub1][lb2--------ub2]
The base address of array =BA
Size of each element in array =c
A[1---4][2---6]
A[1][2] A[1][3] A[1][4] A[1][5] A[1][6]
A[2][2] A[2][3] A[2][4] A[2][5] A[2][6]
A[3][2] A[3][3] A[3][4] A[3][5] A[3][6]
A[4][2] A[4][3] A[4][4] A[4][5] A[4][6]
Memory storage
A[1][2] A[1][3] A[1][4] A[1][5] A[1][6] A[2][2] A[2][3] A[2][4] A[2][5] A[2][6]
. . . . .
100 102 104 106 108 110 112 114 116 118 …….
Row 1 Row 2
If stored in Row Major order, then address of random element A[i][j] is given by
Location [A[i][j]]= BA + [(i-lb1) * (ub2-lb2+1) + (j-lb2)] * c
A[1][2] A[1][3] A[1][4] A[1][5] A[1][6]
A[2][2] A[2][3] A[2][4] A[2][5] A[2][6]
A[3][2] A[3][3] A[3][4] A[3][5] A[3][6]
A[4][2] A[4][3] A[4][4] A[4][5] A[4][6]
Memory storage
A[1][2] A[2][2] A[3][2] A[4][2] A[1][3] A[2][3] A[3][3] A[4][3] A[1][4] A[2][4]
. . . . .
100 102 104 106 108 110 112 114 116 118 …….
column 1 column 2 column 3
If stored in column Major order, then address of random element A[i][j] is given by
Location [A[i][j]]= BA + [(j-lb2) * (ub1-lb1+1) + (i-lb1)] * c
A[2][4]= 100 + [(4-2) * (4-1+1) + (2-1)] * 2 =116
No. of columns just before 4 no of element in each column