An array is a collection of elements, all of the same type, stored in contiguous memory locations. Arrays allow you to store multiple values under a single variable name, which makes it easier to work with large amounts of related data. In C, arrays are one of the most fundamental data structures and are commonly used for handling collections of data, such as lists of numbers, strings, or other types of values.
To declare an array in C, you specify the type of the elements followed by the array's name and the number of elements it will hold, enclosed in square brackets.
Syntax:
type array_name[array_size];
Example:
int numbers[5]; // Declares an integer array of size 5
Here, numbers is an array of integers with space for 5 elements. The index positions for the array elements will be from 0 to 4.
You can initialize an array at the time of declaration or later in your program. In C, arrays are indexed starting from 0.
You can initialize an array with values directly when you declare it. The number of values you specify determines the size of the array.
Syntax:
type array_name[] = {value1, value2, value3, ..., valueN};
Example:
int numbers[] = {10, 20, 30, 40, 50}; // An array of 5 integers
If the array size is not explicitly specified, C will determine the size based on the number of values in the initializer list (in this case, 5).
If you don't initialize all elements, the remaining elements will automatically be initialized to zero (for numeric types).
int numbers[5] = {10, 20}; // Only first two elements initialized, others default to 0
Here, numbers[0] = 10, numbers[1] = 20, and numbers[2], numbers[3], and numbers[4] will be 0.Array elements are accessed using their index, starting from 0. You specify the index inside square brackets to access or modify a particular element.
Syntax:
array_name[index];
Example:
int numbers[] = {10, 20, 30, 40, 50};
printf("%d", numbers[2]); // Output: 30 (accesses the 3rd element)
numbers[3] = 100; // Modifies the 4th element to 100
In the example above, numbers[2] refers to the element at index 2 (which is 30), and numbers[3] = 100 updates the element at index 3 to 100.
C supports multi-dimensional arrays, which are arrays of arrays. The most common example is a two-dimensional array, which can be visualized as a table with rows and columns.
A two-dimensional array is declared by specifying the number of rows and columns.
Syntax:
type array_name[rows][columns];
Example:
int matrix[3][3]; // A 3x3 matrix (3 rows and 3 columns)
You can initialize a 2D array in a similar way to a 1D array, using curly braces for each row.
Syntax:
type array_name[rows][columns] = {{value1, value2, value3}, {value4, value5, value6}, ...};
Example:
int matrix[2][2] = {{1, 2}, {3, 4}}; // A 2x2 matrix
You access elements by specifying both the row and column index.
Syntax:
array_name[row_index][column_index];
Example:
int matrix[2][2] = {{1, 2}, {3, 4}};
printf("%d", matrix[1][1]); // Output: 4 (accesses the element in 2nd row and 2nd column)
In C, an array of strings is an array of character arrays. Each string is represented as an array of characters, with the last character being a null character (\0).
Declaring an Array of Strings:
char strings[3][10]; // An array of 3 strings, each of maximum length 9 (1 for the null terminator)
Initializing an Array of Strings:
char strings[3][10] = {"Hello", "World", "C"};
Accessing Elements:
printf("%s", strings[0]); // Output: Hello
Each string is stored as a character array, and strings[0] refers to the first string, strings[1] refers to the second, and so on.
In C, the size of an array is fixed at the time of declaration and cannot be changed during the execution of the program. However, you can determine the number of elements in a statically allocated array by using the sizeof operator.
Syntax:
int size = sizeof(array_name) / sizeof(array_name[0]);
Example:
int numbers[] = {10, 20, 30, 40, 50};
int size = sizeof(numbers) / sizeof(numbers[0]); // size will be 5
The sizeof(numbers) gives the total size in bytes of the entire array, and sizeof(numbers[0]) gives the size of a single element in the array. Dividing these gives the total number of elements.
Fixed Size: Once an array is created, its size cannot be changed dynamically. This makes arrays less flexible in certain situations.
Memory Allocation: Arrays are allocated in contiguous memory locations, so large arrays may require a significant amount of memory, and inefficient memory usage may result in problems.
No Bounds Checking: C does not perform bounds checking, so accessing an array element outside its declared size can lead to undefined behavior or memory corruption.
You can traverse an array using a loop to access and process each element.
int numbers[] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]); // Output: 10 20 30 40 50
}
To search for an element in an array, you can use a loop to check each element.
int numbers[] = {10, 20, 30, 40, 50};
int target = 30;
int found = 0;
for (int i = 0; i < 5; i++) {
if (numbers[i] == target) {
found = 1;
break;
}
}
if (found) {
printf("Element found\n");
} else {
printf("Element not found\n");
}
Arrays are a fundamental concept in C programming, allowing you to handle multiple pieces of related data efficiently.
Open this section to load past papers