Initialize C arrays while declaring and respect the bounds
C arrays are declared using square brackets next to the array name. As with the other variables, array values should not be used without initializing first. Declaring the array only reserves a space in memory without any guarantee of what is in that space. There is also no bound checking so we have to be more careful when accessing the array elements. So the good programming habits are
- To initialize the array while declaring it using curly braces. Using this approach, we can initialize any single element and rest of the elements will be automatically set to 0.
- Instead of declaring the size using integer constant, use symbolic constants. These constants then can be used in for loops to traverse the array.
- If the array size is not known, it can be computed using
sizeof
operator which return size, in bytes, of the object that follows it.
Why doesn't C automatically do the array bound checking for me?
C gives programmer the flexibility to do what they want efficiently. Adding a logic to do bound check at run time would add to the overhead that C avoids.
Problem
// declare.c -- watch-outs when using arrays
#include <stdio.h>
int main(void)
{
int id[20];
char codes[12];
float score[30];
double marks[99];
// size of operator gives size, in bytes, of the object that follows it
printf("Size of id is %d\n", sizeof id);
printf("Size of codes is %d\n", sizeof codes);
printf("Size of score is %d\n", sizeof score);
printf("Size of marks is %d\n", sizeof marks);
printf("\n");
// array should not be accessed without initializing
printf("Value of first element of id is %d\n", id[0]);
printf("Value of last element of id is %d\n", id[19]);
// array index should not be out of bounds!
printf("Value of out of bound element of id is %d\n", id[20]);
printf("\n");
// assigning a value after declaring an array does not affect other elements
id[0] = 0;
printf("Value of first element of id is %d\n", id[0]);
printf("Value of last element of id is %d\n", id[19]);
return 0;
}
Output
Size of id is 80
Size of codes is 12
Size of score is 120
Size of marks is 792
Value of first element of id is 1138067152
Value of last element of id is 32711
Value of out of bound element of id is -248803352
Value of first element of id is 0
Value of last element of id is 32711
Solution
// declare2.c -- recommended way of declaring and initializing arrays
#include <stdio.h>
#define SIZE 10
int main(void)
{
// initialize using curly braces at the time of declaration
// initializing any one element will initialize entire array to zero
int id[SIZE] = {0};
for (int i = 0; i < SIZE; i++)
printf("id[%d] = %d\n", i, id[i]);
// if size is unknown, determine using sizeof operator
int foo[] = {10, 20, 30};
int length_foo = sizeof foo / sizeof foo[0];
printf("\n");
for (int i = 0; i < length_foo; i++)
printf("foo[%d] = %d\n", i, foo[i]);
return 0;
}
Output
id[0] = 0
id[1] = 0
id[2] = 0
id[3] = 0
id[4] = 0
id[5] = 0
id[6] = 0
id[7] = 0
id[8] = 0
id[9] = 0
foo[0] = 10
foo[1] = 20
foo[2] = 30