Arrays

Declaring, initializing, and accessing elements of arrays. Working with single and multi-dimensional arrays.


Initializing Arrays in C

Arrays in C are contiguous memory locations used to store a collection of elements of the same data type. Before using an array, it's crucial to initialize it. Initialization assigns initial values to the array elements. If you don't initialize an array, it will contain garbage values (whatever was previously stored in those memory locations).

Methods for Initializing Array Elements

There are two primary ways to initialize arrays in C: static initialization during declaration and dynamic initialization using loops.

1. Static Initialization During Declaration

Static initialization involves assigning values to array elements at the time of the array's declaration. This is suitable when you know the initial values beforehand.

Example: Initializing an integer array

 #include <stdio.h>

int main() {
  int numbers[5] = {1, 2, 3, 4, 5}; // Static initialization

  printf("numbers[0] = %d\n", numbers[0]);
  printf("numbers[1] = %d\n", numbers[1]);
  printf("numbers[2] = %d\n", numbers[2]);
  printf("numbers[3] = %d\n", numbers[3]);
  printf("numbers[4] = %d\n", numbers[4]);

  return 0;
} 

Explanation: In this example, an integer array named numbers is declared with a size of 5. The elements are initialized with the values 1, 2, 3, 4, and 5 respectively during declaration.

Example: Initializing a character array (string)

 #include <stdio.h>

int main() {
  char message[] = "Hello"; // Static initialization of a string

  printf("message = %s\n", message);

  return 0;
} 

Explanation: A character array message is initialized with the string "Hello". Notice that we don't explicitly specify the size of the array; the compiler infers it from the string literal (including the null terminator '\0').

2. Dynamic Initialization Using Loops

Dynamic initialization involves assigning values to array elements using loops, typically for loops or while loops. This is useful when the initial values are determined based on some calculation or input.

Example: Initializing an array with squares of numbers

 #include <stdio.h>

int main() {
  int squares[10];
  int i;

  for (i = 0; i < 10; i++) {
    squares[i] = i * i; // Dynamic initialization using a loop
  }

  for (i = 0; i < 10; i++) {
    printf("squares[%d] = %d\n", i, squares[i]);
  }

  return 0;
} 

Explanation: An array squares of size 10 is declared. A for loop iterates from 0 to 9, and in each iteration, the element at index i is assigned the value of i * i (the square of i).

Example: Initializing an array based on user input

 #include <stdio.h>

int main() {
  int grades[5];
  int i;

  printf("Enter the grades for 5 students:\n");
  for (i = 0; i < 5; i++) {
    printf("Grade %d: ", i + 1);
    scanf("%d", &grades[i]); // Dynamic initialization using user input
  }

  printf("\nThe grades entered are:\n");
  for (i = 0; i < 5; i++) {
    printf("Grade %d: %d\n", i + 1, grades[i]);
  }

  return 0;
} 

Explanation: This example prompts the user to enter grades for 5 students. A loop is used to read each grade from the user using scanf and store it in the grades array.

Partial Initialization and its Implications

In C, you can partially initialize an array during declaration. If you provide fewer initializers than the size of the array, the remaining elements are automatically initialized to 0 (for numeric types) or the null character '\0' (for character types).

Example: Partial Initialization

 #include <stdio.h>

int main() {
  int values[10] = {1, 2, 3}; // Partial initialization

  int i;
  for (i = 0; i < 10; i++) {
    printf("values[%d] = %d\n", i, values[i]);
  }

  return 0;
} 

Explanation: The array values of size 10 is declared and partially initialized with 1, 2, and 3. The remaining elements (values[3] to values[9]) are automatically initialized to 0.

Important Implications:

  • Partial initialization can be useful when you want to quickly initialize the beginning of an array and rely on the default initialization for the rest.
  • However, be mindful of this behavior. If you *intend* to initialize all elements but make a mistake, the uninitialized elements will silently be set to 0, which might lead to unexpected program behavior if you're not aware of it.
  • If an array is declared as a global or static variable, even without explicit initialization, it will be initialized to 0 by default. Local variables (declared within functions without the static keyword) need explicit initialization or they'll hold garbage values.