Arrays (1D and Multidimensional)
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
#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[] = "Taro";
printf("Name: %s\n", name); // Outputs 'Name: Taro'.
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;
}
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.