Array
An array is a group of similar elements or data items of the same type
collected at contiguous memory locations. In simple words, we can say
that in computer programming, arrays are generally used to organize the
same type of data.
Array for Integral value:
Indexes 0 1 2 3 4
5 23 10 2 9
Values
Representation of an Array:
Arrays can be represented in several ways, depending on the different
languages. To make you understand, we can take one example of the C
language. The picture below shows the representation of the array.
int array_name[4]={5,6,7,8};
5 6 7 8
0 1 2 3
Arrays always store the same type of values. In the above example:
int is a type of data value.
•
• Data items stored in an array are known as elements.
• The location or placing of each element has an index value.
Important: Array can store only the same type of data items.
Declaration Syntax of Array:
Type VariableName[size];
Example 1: For integral value
int A[5];
Here 5 means, this array A can have 5 integer elements.
10 20 3 4 5
0 1 2 3 4
Example 2: For character value
char B[4];
This array B can have 4 character elements.
A B c d
0 1 2 3
Initialization of an Array:
We can say that we can simply initialize elements of an array at the time
of declaration and for that, we have to use the proper syntax:
Syntax: datatype Array_Name[size] = { value1, value2, value3, ….. valueN
};
Initializer List: To initialize an array in C with the same value, the naive
way is to provide an initializer list. We use this with small arrays.
int num[5] = {1, 1, 1, 1, 1};
This will initialize the num array with value 1 at all index.We may also
ignore the size of the array:
int num[ ] = {1, 1, 1, 1, 1}
The array will be initialized to 0 in case we provide empty initializer list
or just specify 0 in the initializer list.
int num[5] = { }; // num = [0, 0, 0, 0, 0]
int num[5] = { 0 }; // num = [0, 0, 0, 0, 0]
Designated Initializer: This initializer is used when we want to initialize
a range with the same value. This is used only with GCC compilers.
[ first . . . last ] = value;
int num[5]={ [0 . . . 4 ] = 3 };// num = { 3, 3, 3, 3, 3}
We may also ignore the size of array:
int num[ ]={ [0 . . . 4 ] = 3 };// num = { 3, 3, 3, 3, 3}
another example:
int num[5] = {1,[1 ... 4]=3}; // { 1,3,3,3,3}
Macros: For initializing a huge array with the same value we can use
macros.
#include<stdio.h>
#define x1 1
#define x2 x1, 2
#define x4 x2, x2
int main(void)
{
// array declaration
int num[] = {x4,x2,x1};
int size = sizeof(num)/ sizeof(int); // 4+2+1=7
printf("The size of the array is %d\n", size);
printf("The value of element in the array at index 5 is %d
", num[4]);
return 0;
Output:
The size of the array is 7
The value of element in the array at index 5 is 1
Using For Loop: We can also use for loop to initialize an array with the
same value.
#include<stdio.h>
int main(void)
{
int size = 6;
int val = 1;
// array declaration
int arr[size];
int i;
// initializing array elements
for (i = 0; i < size ; i++){
arr[i] = val;
}
// print array
printf("The array is:");
for (i = 0; i < size ; i++){
printf("%d ", arr[i]);
}
return 0;
}
Output:
The array is:1 1 1 1 1 1
#include<stdio.h>
int main(void)
{
int size = 6;
// array declaration
int arr[size];
int i;
// initializing array elements
for (i = 0; i < size ; i++){
scanf("%d",&arr[i]);
}
// print array
printf("The array is:");
for (i = 0; i < size ; i++){
printf("%d ", arr[i]);
}
return 0;
}
Output
1
2
3
4
5
6
The array is 1 2 3 4 5 6
Note: If an array is described inside a function, the elements will have
garbage value. And in case an array is static or global, its elements will
be initialized automatically to 0.