A one-dimensional array stores a sequence of related elements that can be identified using one index. In C, a traditional array has a declared element type and its elements are stored in contiguous memory locations. The index of the first element is 0.
The important idea behind a 1D array is the relationship between an element and its position. If an integer array contains five values, the valid indexes are 0, 1, 2, 3 and 4. This makes an array particularly useful when a program repeatedly needs to read or update values by position.
Index: 0 1 2 3 4
+-----+-----+-----+-----+-----+
Array: | 12 | 25 | 18 | 40 | 31 |
+-----+-----+-----+-----+-----+
For example, arr[2] refers to the third element, whose value is
18 in the diagram above.
The basic C syntax is:
data_type array_name[size];
Example:
int marks[5];
For this array, the valid indexes are 0 through 4. Accessing an index outside this range is an error and can lead to undefined behavior in C.
An array can be initialized when it is declared. The values are written in index order.
int marks[5] = {72, 85, 64, 91, 78};
Here, marks[0] is 72 and marks[4] is 78.
C also permits the compiler to determine the size when an initializer is supplied:
int numbers[] = {10, 20, 30, 40};
The second form creates an array with four elements.
An element can be read or changed directly when its index is known.
#include <stdio.h>
int main()
{
int marks[5] = {72, 85, 64, 91, 78};
printf("Third mark: %d\n", marks[2]);
marks[2] = 70;
printf("Updated third mark: %d\n", marks[2]);
return 0;
}
The access and update operations above use the element's index directly, so they are typically O(1).
Traversal means visiting the elements in sequence. A for loop is a
common choice because the valid indexes form a predictable range.
#include <stdio.h>
int main()
{
int arr[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Output:
10 20 30 40 50
Traversal takes O(n) time because every element may need to be visited.
A simple way to search an unsorted array is linear search. The algorithm checks elements from left to right until the target is found or the array ends.
int arr[6] = {14, 8, 21, 35, 11, 29};
int target = 35;
int position = -1;
for (int i = 0; i < 6; i++)
{
if (arr[i] == target)
{
position = i;
break;
}
}
The worst-case time complexity of linear search is O(n). If the array is sorted, binary search can be considered instead.
A fixed-size C array does not automatically create extra storage when a new value is inserted. If there is free capacity and the program maintains a logical element count, inserting at a position normally requires shifting later elements one place to the right.
Before: 10 20 30 40 Insert 25 at index 2 After: 10 20 25 30 40
The shifting is why insertion in the middle is generally O(n). At the end, when unused capacity is available, the operation can be much simpler.
Deletion from a logical array usually means removing an element and shifting subsequent elements left to close the gap.
Before: 10 20 30 40 50 Delete element at index 2 After: 10 20 40 50
Deleting from the middle can require several shifts and therefore has O(n) worst-case time.
| Operation | Typical Time | Why |
|---|---|---|
| Access by index | O(1) | The position identifies the element directly. |
| Update by index | O(1) | The element can be reached directly. |
| Traversal | O(n) | Elements are visited sequentially. |
| Linear search | O(n) | The target may be at the end or absent. |
| Insertion in middle | O(n) | Elements may need to be shifted. |
| Deletion in middle | O(n) | Elements after the deleted position may shift. |
One reason indexed access is efficient is that the address of an element can be calculated from its position. For a traditional C array, the address follows the concept:
Address of arr[i] = Base Address + (i × size of each element)
For example, if an integer occupies 4 bytes, moving from one integer element to the next advances by 4 bytes. The exact byte size of a type should be obtained from the implementation rather than assumed universally.
Suppose a program stores the marks of five students in one subject. A 1D array matches the problem because every student contributes one value of the same logical type.
#include <stdio.h>
int main()
{
int marks[5] = {72, 85, 64, 91, 78};
int total = 0;
for (int i = 0; i < 5; i++)
{
total += marks[i];
}
printf("Total = %d\n", total);
printf("Average = %.2f\n", total / 5.0);
return 0;
}
This example demonstrates a useful pattern: store related values in the array, then use traversal to calculate a result from the complete collection.
arr[5] for an array of five elements.i <= size instead of i < size in a traversal loop.| Feature | 1D Array | 2D Array |
|---|---|---|
| Indexes | One | Two |
| Typical representation | Sequence | Rows and columns |
| Example | marks[3] |
matrix[2][3] |
| Common use | Lists and sequences | Matrices, grids and tables |
The two-dimensional form deserves separate treatment because row/column indexing and nested-loop traversal introduce different programming patterns.
It is an indexed sequence of elements accessed using one index. In a traditional C array, elements have a declared type and are stored contiguously.
The first element is located at an offset of zero elements from the array's base address, so its index is 0. The next element has an offset of one element and therefore index 1.
Accessing an element by a known index is typically O(1).
Elements after the insertion position may have to be shifted to make room for the new value.
A traditional fixed-size C array cannot automatically resize itself. Dynamic memory allocation or another data structure can be used when the collection size must change.
A one-dimensional array is best understood as an indexed sequence of related elements. Its main strength is efficient access by position, while its main trade-offs are fixed capacity in traditional C arrays and the shifting required for many middle insertions and deletions.
Once indexing, traversal, searching and update operations are clear, students have a strong base for studying two-dimensional arrays, linked lists, stacks, queues and algorithm analysis.