ARRAY IN C
An Array is user define data type. It is collection of several elements of similar data type, where we can store multiple values at a time.
Syntax:
Data Type ArrayName [ No_of_element ];
e.g.
int Arr[5]
Properties of Array:-
1. All elements exist on continues memory locations.
Here i am assuming that it starts from location 100 in memory.
int A[5];
100 102 104 106 108
2.Each element is identified by a number, which called as index no. The index no begins from 0 to N-1.
int A[5];
Here N = 5
0 1 2 3 4
100 102 104 106 108
3.All elements of an array are of same type.
int A[5];
float A[5];
long A[5];
4.Length of array must defined by a constant integer.
int A[5];
= OK
float A[5];
= OK
int A[];
= error
int A[5.7];
= error
int A[l];
= error
int A[l*2];
= error
5.The size of array is sum of size of its elements.
int a[5];
SIZE = 10
float a[200];
SIZE = 800
long a[44];
SIZE = 132
6.All elements of array are automatically initialized by garbaze.
7.Initialization of array:
int a[5] = {10,20,30,40,50};
0 1 2 3 4
10 | 20 | 30 | 40 | 50 |
Partial Initialization-
int a[5] = {10,20,30};
0 1 2 3 4
10 | 20 | 30 | 0 | 0 |
int a[100] = {0};
0………………………………………………………………………………………………………………………………………….99
0 | 0 | 0 | 0 | 0 |
int a[] = {10,20,30,40,50};
10 | 20 | 30 | 40 | 50 |
8.The size of an array also defined by a symbolic constant/macro.
#define sz 25
double A[sz];
int B[sz];
char C[sz];
9.The[]operator,in c language ,is known as sub-script operator.
Disadvantages of array:-
- C environment doesn’t have checking machism for array sizes.
- Different type of data can not be stored into an array.
- We must know in advance that how many elements are to be stored in array.
- Array is static structure. It menas at array is of fixed size. The memory which is allocated to array can not be increased or reduced.
- Since arrayis of fixed size, if we allocate more memory than requirement then, the memory space will be wasted. And if we allocate less memory than requirement, then its will create problem.