Array Fundamentals
Storing data in memory requires allocating space first. To hold 4 integers, you allocate 4 separate int memory blocks:
int data[4];
This allocates 16 bytes total (4 × 4 bytes) and assigns the name data to this collection.
This structure is called an array. Each individual value is an element, and the total count of values is the length. The declaration int data[4]; creates a length-4 integer array named data.
Each element has an index starting from 0 rather than 1. Access elements using:
arrayName[index]
So data[0] is the first element, and data[3] is the fourth element.
Assigning values to the array:
data[0] = 20;
data[1] = 345;
data[2] = 700;
data[3] = 22;
Using Loops with Arrays
Loops are commonly used to populate and read arrays. Here is how to store values 1 through 10:
#include <stdio.h>
int main(){
int values[10];
int idx;
for(idx = 0; idx < 10; idx++){
values[idx] = idx + 1;
}
for(idx = 0; idx < 10; idx++){
printf("%d ", values[idx]);
}
return 0;
}
The loop variable idx serves as both the index and loop counter. Since the array has 10 elements with indices 0 through 9, the loop condition idx < 10 ensures we stop once idx reaches 10.
Reading User Input
To read 10 integers from the user:
#include <stdio.h>
int main(){
int values[10];
int idx;
for(idx = 0; idx < 10; idx++){
scanf("%d", &values[idx]);
}
for(idx = 0; idx < 10; idx++){
printf("%d ", values[idx]);
}
return 0;
}
The scanf function requires a memory address, so the & operator retrieves the address of each array element.
Array Declaration Syntax
dataType arrayName[length];
For example:
float temps[12];
char text[9];
Important rules:
- All elements must share the same data type.
- The length should be a constant expression rather than a variable to ensure portability.
- Index values must fall within
0 ≤ index < length. Out-of-bounds access causes undefined behavior.
Contiguous Memory Layout
Arrays occupy consecutive memory locations with no gaps between elements. This sequential arrangement enables efficient pointer arithmetic and bulk memory operations like copying or filling entire blocks. This property makes arrays suitable for implementing buffers and caches.
Array Initialization
Arrays can be initialized during declaration:
int data[4] = {20, 345, 700, 22};
Values are enclosed in braces and separated by commas.
Partial initialization: When fewer values are provided than elements, only the first elements receive values. The remaining elements default to zero:
int data[10] = {12, 19, 22, 993, 344};
This initializes data[0] through data[4] while data[5] through data[9] become 0.
Default values depend on type:
- Integer types:
0 - Character type:
'\0' - Floating-point types:
0.0
To initialize an entire array to zero, use a single zero value:
int numbers[10] = {0};
char buffer[10] = {0};
float readings[10] = {0.0};
Individual assignment only: You cannot asign values to an entire array at once. All elements must be specified explicitly:
int data[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
Auto-sized arrays: When all elements are initialized, the length can be omitted:
int data[] = {1, 2, 3, 4, 5};
Practical Example: Matrix Output
Displaying a 4×4 matrix:
#include <stdio.h>
int main()
{
int row1[4] = {20, 345, 700, 22};
int row2[4] = {56720, 99, 20098, 2};
int row3[4] = {233, 205, 1, 6666};
int row4[4] = {34, 0, 23, 23006783};
printf("%-9d %-9d %-9d %-9d\n", row1[0], row1[1], row1[2], row1[3]);
printf("%-9d %-9d %-9d %-9d\n", row2[0], row2[1], row2[2], row2[3]);
printf("%-9d %-9d %-9d %-9d\n", row3[0], row3[1], row3[2], row3[3]);
printf("%-9d %-9d %-9d %-9d\n", row4[0], row4[1], row4[2], row4[3]);
return 0;
}