Arrays (1D and Multidimensional)
| Since: | C89(1989) |
|---|
A data structure that stores elements of the same type in contiguous memory. Each element is accessed by its index (subscript). In addition to one-dimensional arrays, multidimensional arrays are available for working with tabular data.
Syntax
// Declares a one-dimensional array.
type arrayName[size];
// Declares and initializes an array at the same time.
type arrayName[size] = {value1, value2, value3};
// Omits the size and lets the compiler determine it from the initializer.
type arrayName[] = {value1, value2, value3};
// Accesses an element (indexes are zero-based).
arrayName[index];
// Declares and initializes a two-dimensional array.
type arrayName[rows][cols] = {{value, value}, {value, value}};
Array Characteristics
| Characteristic | Description |
|---|---|
| Zero-based indexing | The first element has index 0; the last element has index (size - 1). |
| Contiguous memory | Array elements are laid out sequentially in memory, so they can be accessed via pointer arithmetic. |
| Fixed size | The size is fixed at declaration and cannot change at runtime. Use dynamic memory allocation when a variable size is needed. |
| Array name is an address | The array name represents the address of the first element. When passed to a function, it decays to a pointer. |
| Strings are char arrays | A string is represented as an array of char, with a null character ('\0') placed at the end. |
Sample Code
sample_array.c
#include <stdio.h>
int main(void) {
// Declares and initializes a one-dimensional array.
int scores[5] = {80, 75, 90, 65, 88};
// Accesses individual elements by index.
printf("First element: %d\n", scores[0]); // Outputs '80'.
printf("Last element: %d\n", scores[4]); // Outputs '88'.
// Iterates over all elements with a for loop.
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += scores[i];
}
printf("Sum: %d\n", sum); // Outputs 'Sum: 398'.
printf("Average: %.1f\n", (double)sum / 5); // Outputs 'Average: 79.6'.
// Uses sizeof to get the number of elements.
int count = sizeof(scores) / sizeof(scores[0]);
printf("Count: %d\n", count); // Outputs 'Count: 5'.
// Represents a string as a char array.
char name[] = "Yagami Iori";
printf("Name: %s\n", name); // Outputs 'Name: Yagami Iori'.
printf("Length: %zu\n", sizeof(name) - 1); // Length excluding the null character.
// Declares and initializes a two-dimensional array.
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
// Displays all elements using nested loops.
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// Output: 1 2 3 / 4 5 6
return 0;
}
Run the following command:
gcc array.c -o array ./array First element: 80 Last element: 88 Sum: 398 Average: 79.6 Count: 5 Name: Yagami Iori Length: 11 1 2 3 4 5 6
Common Mistakes
Common Mistake: Out-of-Bounds Access
Accessing an array out of bounds causes undefined behavior. C does not perform boundary checking.
array_oob_ng.c
#include <stdio.h>
int main(void) {
int arr[5] = {1, 2, 3, 4, 5};
printf("%d\n", arr[5]); /* index 5 is out of bounds for a 5-element array */
return 0;
}
Run the following command:
gcc array_oob_ng.c -o array_oob_ng ./array_oob_ng -1735289080
Reading beyond the array boundary returns an indeterminate value. Always keep indexes within the range of 0 to less than the element count.
array_oob_ok.c
#include <stdio.h>
int main(void) {
int arr[5] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n; i++) {
printf("%d\n", arr[i]);
}
return 0;
}
Run the following command:
gcc array_oob_ok.c -o array_oob_ok ./array_oob_ok 1 2 3 4 5
Notes
C arrays do not perform bounds checking. Accessing an array out of bounds causes undefined behavior — the program may crash or corrupt data in another memory region. Always ensure indexes stay within the valid range.
Arrays and pointers are closely related. scores[i] is equivalent to *(scores + i). For details on how arrays are handled when passed to functions, see Pointers and Arrays.
To determine the size at runtime, use dynamic memory allocation (malloc()). Variable-length arrays (VLAs) are also available since C99, but avoid them for large sizes due to the risk of stack overflow.
If you find any errors or copyright issues, please contact us.