Arrays

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


Declaring Arrays in C

Arrays are fundamental data structures in C used to store a collection of elements of the same data type under a single variable name. They provide a convenient way to organize and manipulate related data. This document explains how to declare arrays in C, including specifying the data type, size, and different ways to initialize them.

What is an Array?

An array is a contiguous block of memory locations used to store a fixed-size sequential collection of elements of the same type. Each element in the array is accessed using its index, which starts from 0 for the first element.

Syntax for Declaring Arrays in C

The general syntax for declaring an array in C is as follows:

data_type array_name[array_size];
  • data_type: Specifies the data type of the elements that the array will hold (e.g., int, float, char).
  • array_name: The name you choose for the array variable.
  • array_size: The number of elements the array can store. This must be a constant expression (e.g., a literal integer or a #defined constant) known at compile time.

Examples of Array Declarations with Different Data Types

Here are some examples demonstrating how to declare arrays with different data types:

Integer Arrays (int)

This declares an integer array named numbers that can store 5 integers:

int numbers[5];

Floating-Point Arrays (float)

This declares a floating-point array named temperatures that can store 10 floating-point numbers:

float temperatures[10];

Character Arrays (char)

Character arrays are often used to store strings. This declares a character array named name that can store up to 20 characters (plus the null terminator for strings):

char name[20];

Double Arrays (double)

This declares a double array named `prices` that can store 8 double-precision floating point numbers

double prices[8];

Short Arrays (short)

This declares a short array named `ages` that can store 12 short integers

short ages[12];

Specifying the Array Size

The array_size is a crucial part of the array declaration. It determines the amount of memory allocated to the array. It must be a constant expression that can be evaluated at compile time. This means you cannot use a variable to specify the size directly within the array declaration (unless using Variable Length Arrays - explained later, but generally discouraged for beginners).

Example using a #define constant:

#define ARRAY_SIZE 100

int data[ARRAY_SIZE];

In this example, ARRAY_SIZE is a preprocessor macro that is replaced with 100 during compilation, making it a constant expression.

Array Initialization During Declaration

Arrays can be initialized during declaration by providing a list of values enclosed in curly braces {}. If the number of initializers is less than the array size, the remaining elements are initialized to 0 (for numeric types) or null characters (for character arrays). If the array size is omitted during initialization, the compiler automatically determines the size based on the number of initializers.

Examples:

Initializing an Integer Array:

int numbers[5] = {1, 2, 3, 4, 5}; // Initializes all 5 elements
int scores[] = {85, 92, 78, 95};   // Compiler infers size as 4
int partial[5] = {10, 20};        // Initializes the first two elements, remaining elements are 0

Initializing a Character Array:

char message[] = "Hello"; // Initializes a character array with the string "Hello" (size is 6 including the null terminator)
char letters[3] = {'A', 'B', 'C'}; // Initializing each element individually
char empty[10] = {'\0'}; // An empty string. Only the first character is explicitly initialized to NULL, the rest is initialized to zero/NULL automatically. 

Initializing a Float Array:

float values[3] = {1.5, 2.7, 3.9};
float more_values[] = {2.0f, 4.0f, 6.0f, 8.0f}; // Compiler infers size as 4, note the 'f' suffix 

Important Considerations

  • Array Indexing: Remember that array indexing starts from 0. So, for an array of size n, the valid indices are from 0 to n-1. Accessing an array element outside this range results in undefined behavior (e.g., program crash, incorrect results, security vulnerabilities).
  • Memory Allocation: When you declare an array, the compiler allocates a contiguous block of memory to store the elements. The amount of memory allocated is determined by the data type and the size of the array (sizeof(data_type) * array_size).