Characteristics of an Array in C Programming
Characteristics of an Array
-
The Declaration int a[5] is nothing but the creation of five variable of integer types in memory. Instead of declaring five variables for five values, the programmer can define them in an array.
-
All the elements of an array share the same name, and they are distinguished from one another with the help of the number of the element.
-
The elements number in an array plays a major role in calling each element.
-
Any particular elements of an array can be modified separately without disturbing the other elements.
Int a [5] = {1,2,3,4,8};
If a programmer needs to replace 8 with 10, then it need not require changing all other numbers expects 8. To carry out this task, the statement a [4]=10 can be used. Here the other four elements are not disturbed.
5. Any element of an array a [] can be assigned/equated to another ordinary variable or array variable of its type.
Example:-
b=a [2] ;
a [2]=a [3] ;
-
In the statement b=a [2] or vice versa, the value of a [2] is assigned to ‘b’ , where ‘b’
is an integer.
b) In the statement a [2] =a [3] or vice versa, the value of a [2] is assigned to a [3],
where both the elements are of the same array.
c) The array elements are stored in contiguous memory locations.
6. Array elements are stored in contiguous memory locations,
A program on array initialization and determination of their memory locations is given below:
-
WAP display array elements with their addresses.
Int main()
{
int num [5] = {1,2,3,4,5} ;
clrscr();
printf (“\n num[0] = %d address : %u”,num [0] , &num[0]);
printf (“\n num[1] = %d address : %u”,num [1] , &num[1]);
printf (“\n num[2] = %d address : %u”,num [2] , &num[2]);
printf (“\n num[3] = %d address : %u”,num [3] , &num[3]);
printf (“\n num[4] = %d address : %u”,num [4] , &num[4]);
}
Output:
num [0] 1 address : 65516
num [1] 2 address : 65518
num [2] 3 address : 65520
num [3] 4 address : 65522
num [4] 5 address : 65524
7. Once the array is declared, its lowest boundary cannot be changed but the upper boundary can be expanded. The array name itself is a constant pointer and we cannot modify it. Therefore, the lowest boundary of an array cannot be expanded. In other words, even if the boundary exceeds than specified, nothing happens. The compiler throws no errors.
8. We know that an array name itself is a pointer. Through it is a pointer, it does not need ‘*’ operator. The brackets ([]) automatically denote that the variable is a pointer.
9. All the elements of an array share the same name, and they are distinguished from one another with the help of the number of the element.
10. The amount of memory required for an array depends upon the data type and the number of elements. The total size in bytes for a single-dimensional array is computed.
Total bytes = size of (data type) x size of array
11. when an array is declared and not initialized, it contains garbage values. If we declared an array as static, all elements are initialized to zero.