C Arrays
Defination
In C, an array is a sequence of elements of the same type, arranged in adjacent memory slots and defined with a fixed length at compile time. Arrays serve as a structured means to handle multiple values under a single identifier, optimizing storage and access.
Declaration and Syntax
To create an array, specify the type, followed by the array name, and enclose the desired capacity in square brackets:
int scores[5];
Here, scores is a container for five integers, indexed from 0 to 4.
Storage and Memory Layout
Each element resides in sequential memory addresses, meaning the address of one item is directly followed by the next. This layout allows for efficient index-based access, but it also means the array size must be known ahead of time.
Initialization Techniques
Arrays can be populated during definition:
float values[3] = {3.5, 4.2, 5.1};Alternatively, C permits partial initialization, filling remaining slots with default zero values:
char letters[4] = {'a', 'b'};Uninitialized elements here become '\0'.
Accessing Components
You access individual elements by their position, beginning with index 0 as the first element.
printf("%d", scores[2]); // Access third itemReading or updating is straightforward via this index mechanism.
Modifying Values
Changing the contents of an array item uses direct assignment:
scores[1] = 90;
This updates the second element to 90.
Multidimensional Arrays
For tabular data, C offers arrays of arrays (matrices):
int grid[2][3] = {
{1, 2, 3},
{4, 5, 6}
}; You reference an item using multiple indices: grid[1][2] gives 6.
- Size rigidity: Array size is fixed upon creation.
- No boundary checks: C doesn't prevent out-of-bounds access.
- Cannot grow/shrink: Arrays are static—dynamic resizing requires pointers and memory management.
Passing Arrays to Functions
When arrays are sent to functions, they decay into pointers to the first element:
void display(int arr[], int length);
Within the function, you can iterate over the array using the given size.
Pointer Interactions
Arrays and pointers share a close relationship in C:
int *p = scores;
You can traverse the array with pointer arithmetic:
*(p + 1) = 75;
This assigns a value to the second item in the array using pointer arithmetic.
String as Character Arrays
Strings in C are implemented as null-terminated arrays of characters:
char word[] = "code";
This stores 'c', 'o', 'd', 'e', and '\0'.
Array of Pointers
Useful for string lists or dynamic memory scenarios:
char *cities[] = {"Paris", "Tokyo", "Delhi"};Each item in this array holds the address of a string constant.
Quick Summary of C Arrays
| Feature | Description |
|---|---|
| Indexing | Starts from 0 |
| Uniform Data Type | Every element in the array must be of an identical data type. |
| Static Size | Fixed size once declared |
| Access Method | Via index or pointer arithmetic |
| Multidimensional | Supported with matrix-like syntax |
| Function Passing | When passed to functions, arrays are represented by pointers pointing to their initial element. |
Prefer Learning by Watching?
Watch these YouTube tutorials to understand C Tutorial visually:
What You'll Learn:
- 📌 #19 C Arrays | [2025] C Programming For Beginners
- 📌 C_46 Arrays in C - part 1 | Introduction to Arrays