Declaration of One Dimensional Array:
1 |
data_type array_name[array_size]; |
For example:
1 |
int arr[5]; |
Above example is an one dimensional array. Here the name of an array is arr with array size 5. int specifies the type of variable. The [5] tells how many elements in the array. The array_size must be an integer constant greater than zero.
The array_name itself is a constant pointer pointing to the base address of an array. The base address of the array is the address of the first element of an array.
Initialisation of One-Dimensional Array:
Arrays can be initialized at declaration time. You can initialize an element of an array, one by one or in a single statement. The number of an element inside of braces { } should not be greater than array_size.
1 |
int arr[5] = {4,10,55,9,2}; |
Initializing each element separately in a loop:
1 2 3 4 5 6 |
int arr[5]; int i = 0; // Initializing elements of array separately for(i=0;i <= 5;i++) { arr[i] = i; } |
Points to remember:
-
1int arr[5] = {10, 20};
Here array size is 5 and number of elements is 2, so remaining elements are set to zero.
This phenomenon is called as partial initialization of array. -
1int arr[] = {4, 10, 55, 9, 2};
Mentioning the dimension of the array is optional. - To find the size of an entire array, sizeof is used. Therefore, in above example sizeof(arr) is 20 bytes.
This is because array elements are 5 of integer type therefore 5*4=20.
The size of each element, sizeof(arr[0]) is 4 bytes because array is of integer type. -
12int size=5;int arr[size]; //not allowed
The array_size should not be a variable name.
Accessing array elements using subscript:
An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array.
For example:
1 |
int arr[5] = {1,2,3,4,5}; |
values are of int data type, whereas addresses are of int* type.
&arr[0] :- 100 – address of the first element of the array.
&arr[1] :- 104 – address of the second element of the array.
&arr[2] :- 108 – address of the third element of the array.
&arr[3] :- 112 – address of the forth element of the array.
&arr[4] :- 116 – address of the fifth element of the array.
arr[0] :- 1 – retrive value of the first element of the array.
arr[1] :- 2 – retrive value of the second element of the array.
arr[2] :- 3 – retrive value of the third element of the array.
arr[3] :- 4 – retrive value of the forth element of the array.
arr[4] :- 5 – retrive value of the fifth element of the array.
scanf(“%d”,&arr[2]); //statement to insert value in the third element of array arr.
printf(“%d”,arr[0]); //statement to print first element of an array.
C Array Example:
C program to find the sum of marks of n students using array subscript notation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> int main() { int marks[10], sum=0, i, n; printf("Enter number of students:\t"); scanf("%d",&n); for(i=0;i<n;++i) { printf("Enter marks of student%d\n", i+1); scanf("%d",&marks[i]); sum = sum + marks[i]; } printf("Sum=%d",sum); return 0; } |
Output:
1 2 3 4 5 6 7 8 |
Enter number of students: 3 Enter marks of student1 57 Enter marks of student2 40 Enter marks of student3 66 Sum=163 |
Accessing array with Pointer:
We can also access elements of an array using pointers. Name of the array itself is a pointer pointing to the base address of an array.
Suppose for example:
1 |
int arr[3] = {1,2,3}; |
Now by using following terms in the program, we can access array elements.
address of an element can be written as:
address = (array_name + index*sizeof(data_type))
& arr[1]=(arr + 1 x 4)=104
(arr+0) :- 100 – address of the first element of the array.
(arr+1) :- 104 – address of the second element of the array.
(arr+2) :- 108 – address of the third element of the array.
*(arr+0) :- ‘1’ -retrieve value of the first element of the array.
*(arr+1) :- ‘2’ -retrieve value of the second element of the array.
*(arr+2) :- ‘3’ -retrieve value of the third element of the array.
scanf(“%d”,(arr+2)); //statement to insert value in the third element of array arr[].
printf(“%d”,(arr+0); //statement to print first element of an array.
Here arr+1 gives an address of the second element of the array arr. Because base address of arr is 100 and 1 is index(i.e 4 bytes) therefore 100+4=104
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
int array[3] = {58, 96, 66}; //To get first element printf("first element is = %d", array[0]); //58 printf("first element is = %d", 0[array]); //58 printf("first element is = %d", *array); //58 printf("first element is = %d", *(array+0)); //58 printf("first element is = %d", *(0+array)); //58 //To get address of first element printf("address of first element is = %d", array); //2000 printf("address of first element is = %d", (array + 0)); //2000 printf("address of first element is = %d", (0+array)); //2000 //To get second element printf("second element is = %d", array[1]); //96 printf("second element is = %d", 1[array]); //96 printf("second element is = %d", *(array+1)); //96 printf("second element is = %d", *(1+array)); //96 //To get address of second element printf("address of second element is = %d", (array + 1)); //2004 printf("address of second element is = %d", (1+array)); //2004 //To get third element printf("thirdt element is = %d", array[2]); //66 printf("third element is = %d", 2[array]); //66 printf("third element is = %d", *(array+2)); //66 printf("third element is = %d", *(2+array)); //66 //To get address of third element printf("address of third element is = %d", (array + 2)); //2008 printf("address of third element is = %d", (2+array)); //2008 |
C Array Example:
C program to find the sum of marks of n students using array using the pointer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> int main() { int marks[10], sum = 0, i, n; printf("Enter number of students:\t"); scanf("%d",&n); for(i = 0; i < n; ++i) { printf("Enter marks of student%d,\n", i+1); scanf("%d", (marks + i)); sum = sum + *(marks + i); } printf("Sum = %d", sum); return 0; } |
Output:
1 2 3 4 5 6 7 8 |
Enter number of students: 3 Enter marks of student1 57 Enter marks of student2 40 Enter marks of student3 66 Sum = 163 |