0% found this document useful (0 votes)
6 views

Array Part -2

The document explains the concepts of pointers and arrays in C, detailing how arrays are essentially pointers to their first element and how to access their elements using both array notation and pointer arithmetic. It covers the calculation of addresses for elements in both one-dimensional and two-dimensional arrays, including how to pass arrays to functions. Additionally, it discusses the memory storage order (row-major and column-major) for multi-dimensional arrays and provides examples of array initialization and traversal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Array Part -2

The document explains the concepts of pointers and arrays in C, detailing how arrays are essentially pointers to their first element and how to access their elements using both array notation and pointer arithmetic. It covers the calculation of addresses for elements in both one-dimensional and two-dimensional arrays, including how to pass arrays to functions. Additionally, it discusses the memory storage order (row-major and column-major) for multi-dimensional arrays and provides examples of array initialization and traversal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Pointers & Arrays:

Let a[5] be an array with 5 elements.

BA= 100 102 104 106 108

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]

Address of an element i of array a = a + i * sizeof(element)

#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

a[4]= 100+ (4)*2=100+8=108


a[4]= 100 +(4-0)*2=108
Example: int a[5]={12,4,7,9,10};
a[3---7]

100
BA= 102 104 106 108

12 4 7 9 10
a[3] a[4] a[5] a[6] a[7]

100

a[6]= 100 +(6-3)*2=100+(3)*2=106


Passing array to a function
1. #include <stdio.h>
2. void getarray(int arr[])
3. {
4. printf("Elements of array are : ");
5. for(int i=0;i<5;i++)
6. {
7. printf("%d ", arr[i]);
8. }
9. }
10.int main()
11.{
12.int arr[5]={45,67,34,78,90};
13.getarray(arr);
14.return 0;
15.}

Output:
Elements of array are 45 67 34 78 90

Passing array to a function as a pointer


1. #include <stdio.h>
2. void printarray(int *a)
3. {
4. printf("Elements of array are : ");
5. for(int i=0;i<5;i++)
6. {
7. printf("%d ", a[i]);
8. }
9. }
10.int main()
11.{
12.int arr[5]={2,3,4,5,6};
13.printarray(arr);
14.return 0;
15.}
Output:
Elements of array are 2 3 4 5 6
Multi-Dimensional Array

In C language, one can have arrays of any dimensions. Arrays can be 1-dimensional, 2-
dimensional, 3-dimensional, etc.

Two Dimensional Array in C


The two-dimensional array can be defined as an array of arrays. The 2D array is organized
as matrices which can be represented as the collection of rows and columns. However, 2D
arrays are created to implement a relational database lookalike data structure. It provides
ease of holding the bulk of data at once which can be passed to any number of functions
wherever required.
Declaration of two dimensional Array in C
The syntax to declare the 2D array is given below.
data_type array_name[rows][columns];
Consider the following example.
int twodimen[4][3];

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

printing the elements ....

56 10 30
34 21 34
45 56 78

Let b be the Two Dimensional Array b[i][j]

For Row Major Order: Size of b[i][j] = b + ( Number of rows * i + j )*sizeof(element)

For Column Major Order: Size of b[i][j] = b + ( Number of Columns * j + i


)*sizeof(element)

*(*(b + i) + j) is equivalent to b[i][j]

*(b + i) + j is equivalent to &b[i][j]

*(b[i] + j) is equivalent to b[i][j]

b[i] + j is equivalent to &b[i][j]


(*(b+i))[j] is equivalent to b[i][j]

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]

Number of rows= ub1-lb1+1= 4-1+1=4


Number of columns= ub2-lb2+1 = 6-2+1=5

Row Major order:

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]

A[2][5]= 100 + [(2-1) * (6-2+1) + (5-2)] * 2 =116


No. of rows just before 2 no of element in each row

No. of element to be skipped before row 2

Column Major order:

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

No. of element to be skipped before column 4

You might also like