An array is a data structure used to process multiple elements with the same data type when a number of such elements are known. You would use an array when, for example, you want to find out the average grades of a class based on the grades of 50 students in the class. Here you cannot define 50 variables and add their grades. This is not practical. Using an array, you can store grades of 50 students in one entity, say grades, and you can access each entity by using subscript as grades[1], grades[2]. Thus you have to define the array of grades of the float data type and a size of 50. An array is a composite data structure; that means it had to be constructed from basic data types such as array integers.
Program
#include main() { int a[5]; \A for(int i = 0;i<5;i++) { a[i]=i;\B } printarr(a); } void printarr(int a[]) { for(int i = 0;i<5;i++) { printf("value in array %d ",a[i]); } }
Explanation
Note |
The array subscript has the highest precedence among all operators thus a[1] * a[2] gives the multiplication of array elements at position 1 and position 2. |
Points to Remember
Each element of the array has a memory address. The following program prints an array limit value and an array element address.
Program
#include void printarr(int a[]); main() { int a[5]; for(int i = 0;i<5;i++) { a[i]=i; } printarr(a); } void printarr(int a[]) { for(int i = 0;i<5;i++) { printf("value in array %d ",a[i]); } } void printdetail(int a[]) { for(int i = 0;i<5;i++) { printf("value in array %d and address is %16lu ",a[i],&a[i]); \ A } }
Explanation
Point to Remember
For array elements, consecutive memory locations are allocated.
You can access an array element by using a pointer. For example, if an array stores integers, then you can use a pointer to integer to access array elements.
Program
#include void printarr(int a[]); void printdetail(int a[]); main() { int a[5]; for(int i = 0;i<5;i++) { a[i]=i; } printdetail(a); } void printarr(int a[]) { for(int i = 0;i<5;i++) { printf("value in array %d ",a[i]); } } void printdetail(int a[]) { for(int i = 0;i<5;i++) { printf("value in array %d and address is %8u ",a[i],&a[i]); } } void print_usingptr(int a[]) \ A { int *b; \ B b=a; \ C for(int i = 0;i<5;i++) { printf("value in array %d and address is %16lu ",*b,b); \ D b=b+2; \E } }
Explanation
Points to Remember
When the pointer is incremented by an increment operator, it is always right incremented. That is, if the pointer points to an integer, the pointer is incremented by 2, and, if it is long, it is incremented by 4.
Program
#include void printarr(int a[]); void printdetail(int a[]); void print_usingptr(int a[]); main() { int a[5]; for(int i = 0;i<5;i++) { a[i]=i; } print_usingptr(a); } void printarr(int a[]) { for(int i = 0;i<5;i++) { printf("value in array %d ",a[i]); } } void printdetail(int a[]) { for(int i = 0;i<5;i++) { printf("value in array %d and address is %8u ",a[i],&a[i]); } } void print_usingptr(int a[]) { int *b; b=a; for(int i = 0;i<5;i++) { printf("value in array %d and address is %16lu ",*b,b); b++; // A } }
Explanation
Point to Remember
The increment operator increments the pointer according to the size of the data type.
You can put values in the memory locations by using pointers, but you cannot assign the memory location to an array to access those values because an array is a pointer constant.
Program
#include void printarr(int a[]); void printdetail(int a[]); void print_usingptr_a(int a[]); main() { int a[5]; int *b; int *c; for(int i = 0;i<5;i++) { a[i]=i; } printarr(a); *b=2; \ A b++; \ B *b=4; \ C b++; *b=6; \ D b++; *b=8; \ E b++; *b=10; b++; *b=12; b++; a=c; //error \F printarr(a); } void printarr(int a[]) { for(int i = 0;i<5;i++) { printf("value in array %d ",a[i]); } } void printdetail(int a[]) { for(int i = 0;i<5;i++) { printf("value in array %d and address is %16lu ",a[i],&a[i]); } } void print_usingptr_a(int a[]) { for(int i = 0;i<5;i++) { printf("value in array %d and address is %16lu ",*a,a); \ F a++; // increase by 2 bytes \ G } }
Explanation
Point to Remember
The array limit is a pointer constant and you cannot change its value in the program.
You can define two- or multi-dimensional arrays. It is taken as an array of an array. Logically, the two-dimensional array 3 X 2 is taken as
3 1 5 2 8 7
Here there are three arrays, i.e. one array in each row. The values are stored as
3 1 5 2 8 7
This style is called row measure form. Each row array is represented as a[0], which consists of elements 3 and 1. a[1] consists of 5 2 and a[2] consists of 8 7. Each element of a[0] is accessed as a [0] [0] and a[0] [1], thus the value of a[0][0] and a[0][1] is 1.
Program
#include void printarr(int a[][]); void printdetail(int a[][]); void print_usingptr(int a[][]); main() { int a[3][2]; \ A for(int i = 0;i<3;i++) for(int j=0;j<2 ;j++) { { a[i]=i; } } printdetail(a); } void printarr(int a[][]) { for(int i = 0;i<3;i++) for(int j=0;j<2;j++) { { printf("value in array %d ",a[i][j]); } } } void printdetail(int a[][]) { for(int i = 0;i<3;i++) for(int j=0;j<2;j++) { { printf( "value in array %d and address is %8u ", a[i][j],&a[i][j]); } } } void print_usingptr(int a[][]) { int *b; \ B b=a; \ C for(int i = 0;i<6;i++) \ D { printf("value in array %d and address is %16lu ",*b,b); b++; // increase by 2 bytes \ E } }
Explanation
Points to Remember
Just as with a two-dimensional array, you can define three-dimensional arrays and processing in a similar way. Each three-dimensional array is taken as an array of two-dimensional arrays.
Program
#include main() { int a[2][3][4]; \ A int b[3][4]; \ B int c[4]; \ C int cnt=0; for(int i=0;i<2;i++) for(int j=0;j<3;j++) for(int k=0;k<4;k++) { a[i][j][k] = cnt; cnt; } } void print_onedim(int a[]) \ D { for(int i=0;i<4;i++) printf("%d ",a[i]); } void print_twodim(int a[][4]) \ E { for(int j=0;j<3;j++) print_onedim(a[j]); printf(" "); } void print_threedim(int a[][3][4]) \ F { printf("Each two dimension matrix "); for(int j=0;j<2;j++) print_twodim(a[j]);; }
Explanation
int a[3][4]
it is considered as three arrays of the size 4. So the base address of a[0] is a itself. The base address of a[1] is a+8 because the first row has 4 elements of size 2 bytes each; thus we can get the base address of a[1]. Similarly, the base address of a[2] is a+16. Thus, to calculate the base address, you should know the inner dimension, 4.
Points to Remember
You can define a pointer array similarly to an array of integers. In the pointer array, the array elements store the pointer that points to integer values.
Program
#include void printarr(int *a[]); void printarr_usingptr(int *a[]); int *a[5]; \ A main() { int i1=4,i2=3,i3=2,i4=1,i5=0; \ B a[0]=&i1; \ C a[1]=&i2; a[2]=&i3; a[3]=&i4; a[4]=&i5; printarr(a); printarr_usingptr(a); } void printarr(int *a[]) \ D { printf("Address Address in array Value "); for(int j=0;j<5;j++) { printf("%16u %16u %d ", a[j],a[j],a[j]); \E } } void printarr_usingptr(int *a[]) { int j=0; printf("using pointer "); for( j=0;j<5;j++) { printf("value of elements %d %16lu %16lu ",**a,*a,a); \ F a++; } }
Explanation
Point to Remember
You can store pointers in arrays. You can access values specified by these values by using the * notations.
Part I - C Language
Part II - Data Structures
Part III - Advanced Problems in Data Structures