Array is a term used for the collection of similar data type elements.
An array is a secondary data type derived from fundamental.It must have data type int, char, double float, struct or union but every element under an array identifier must have the similar data type.
Suppose we required 5 integer elements in a program then there are two ways to allocate memory:
— Declare 5 different variables to store data.
1 2 3 4 5 |
int val1=10; int val2=20; int val3=30; int val4=40; int val5=50; |
— Declare an array to store 5 values under a single variable name.
1 |
int arr_num[5]={10,20,30,40,50}; |
datatype array_Variable_Name[Max_Element_Count];
1 |
int arr_num[5]; |
Pronounce above declaration as “arr_num is an array of 5 integer datatype elements”.
int = array elements data type
arr_num= array variable or identifier
[5]=number of elements
1 |
char arr_var[5]; |
Pronounce above declaration as “arr_var is an array of 5 character type elements”.
char = array elements data type
arr_var= array variable or identifier
[5]=number of elements
1 |
float arr_var[5]; |
Pronounce above declaration as “arr_var is an array of 5 float or decimal datatype elements”.
float= array elements data type
arr_var= array variable or identifier
[5]=number of elements
1 2 3 4 5 6 |
struct employee { unsigned char name[20]; unsigned long int empId; double salary; }emp[5]; |
Pronounce above declaration as “emp is an array of 5 employee type structure”.
struct employee = data type
emp= structure variable/identifier
[5]=number of elementsA declared array may have some garbage data at the allocated memory location. So to be on safe side it is best practice to initialize the array with zero value at the time of declaration.
datatype array_Variable_Name[Max_Element_Count]={val1,val2,val3,…….valn};
OR
datatype array_Variable_Name[ ]={val1,val2,val3,…….valn};
In Syntax 1 Number of elements in the array cannot exceed beyond the declared Max_Element_Count so
array size = Max_Element_Count*sizeof(dataType).
In Syntax 2 Number of elements in the array is not fixed so the size of the array will be-
array size=number of initialized array elements*sizeof(dataType).
— Initialize an array of 5 integer datatype elements with value 10,20,30,40,50.
1 2 3 4 5 |
int arr_num[5]={10,20,30,40,50}; Or int arr_num[]={10,20,30,40,50}; |
— Declare An array of 5 character datatype elements.
1 |
char arr_var[5]={'A','M','I','T'}; |
— Declare An array of 5 decimal datatype elements.
1 |
float arr_var[5]={4.34,6.88,1.00,5}; |
— Declare An array of structure for 5 employee information.
1 2 3 4 5 6 |
struct employee { unsigned char name[20]; unsigned long int empId; double salary; }emp[5]={"Amit",32767,75000.67,"Mayank",56544,54567.564}; |
1 2 3 4 5 6 7 8 9 10 11 12 |
#include<stdio.h> int main() { char name[5]={'A','m','i','t'}; int i; for(i=0;i<5;i++) { printf("Address of %dth index = %p\n",i,&name[i]);/*%p is use for Address print in Hex format*/ } return 0; } |
1 2 3 4 5 |
Address of 0th index = 0x7ffd2696d6e0 Address of 1th index = 0x7ffd2696d6e1 Address of 2th index = 0x7ffd2696d6e2 Address of 3th index = 0x7ffd2696d6e3 Address of 4th index = 0x7ffd2696d6e4 |
You can see the difference between the continuous addresses is 1 byte because the array is char data type and char data type size is 1 byte, for int and float it will be 4 bytes because of their memory size.
1 |
int arr_num[5]={10,20}; |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include<stdio.h> int main() { int arr[5]={10,20,30,40,50,60,10,20,30,40,50}; int i; for(i=0;i<10;i++) { printf("%d, ",arr[i]); } return 0; } |
When compiling this program, compiler will execute program with following warning-
1 2 3 4 5 6 |
warning: excess elements in array initializer int arr[5]={10,20,30,40,50,60,10,20,30,40,50}; Output: 10, 20, 30, 40, 50, 32766, 2114791168, -221224951, 4195872, 0, |
See the output.Data is correct till 5 elements and after that, some garbage values received because array reserved memory only for 5 elements.
Note: Sometimes it causes segmentation fault also.
1 2 3 4 5 6 7 8 9 10 |
#include<stdio.h> int main() { int arr[5]={10,20,30}; printf("Size of array is =%ld",sizeof(arr));/*size of arr=size*sizeof(int)=5*4=20*/ return 0; } |
1 2 3 |
Output: Size of array is =20 |
So you can understand Array size calculation like this –
size of arr = size*sizeof(int) = 5 X 4 = 20