Understanding Single Dimensional Array (1D Array) in C | Unit 4 | BKNMU BCA
BKNMU Junagadh | BCA Sem 1 | Unit 4
Understanding Single Dimensional Array (1D Array) in C
Complete Guide to Linear Data Structures, Memory Allocation, Loop-based Input/Output, and Fundamental Operations
A 1D array is defined by four core mathematical and structural behaviors:
1. One-Dimensional Access
Every element in the list is uniquely identified using only one index number (subscript) inside a single set of brackets arr[index].
2. Linear Memory Layout
Elements reside side-by-side in uninterrupted memory addresses, forming a simple linear list (locker row analogy).
3. Byte-Step Access
Moving from index i to i+1 shifts the memory pointer by exactly sizeof(data_type) bytes (e.g., 2 or 4 bytes for int).
4. Optimized Traversals
Because elements are contiguous and indexed linearly, using simple single for loops is the most efficient method for input and output processing.
A complete reference of syntax, memory flow, and coding loops for fundamental 1D operations:
| Operation Type | Syntax / Pattern Examples | Description & Memory Action |
|---|---|---|
| Declaration | data_type arr_name[SIZE];int marks[10]; |
Compiler reserves SIZE × sizeof(type) contiguous bytes. Holds garbage values. |
| Initialization | int arr[3] = {10, 20, 30};char vowels[] = {'A', 'E', 'I'}; |
Compiler sets memory contents during program loading. Contiguous blocks populated sequentially. |
| Accessing Element | x = arr[2];arr[4] = 99; |
CPU fetches/writes data at address (Base_Addr + 2*sizeof(type)) in constant time $O(1)$. |
| Loop Input (scanf) | for(i=0; i<SIZE; i++) scanf("%d", &arr[i]); |
Sequential user data population. Pointer steps through the list address-by-address. |
| Loop Output (printf) | for(i=0; i<SIZE; i++) printf("%d\t", arr[i]); |
Sequential value retrieval and display. Pointer steps through the list value-by-value. |
- Formula:
Total Bytes = Number of Elements × sizeof(Single Element Type) - Example: A
float prices[50];array on GCC (wherefloatis 4 bytes) occupies50 × 4 = 200 Bytesof contiguous RAM.
The following program demonstrates initializing a 1D array using a loop, traversing it to multiply elements by a factor, and displaying the results:
#include <stdio.h>
int main() {
// Declaration of 1D array of size 5
int numbers[5];
int i, factor;
// 1. Initialize Array using for loop (populating squares)
for (i = 0; i < 5; i++) {
numbers[i] = (i + 1) * (i + 1); // index 0 stores 1, 1 stores 4...
}
// Display original array contents
printf("Original numbers array:\n");
for (i = 0; i < 5; i++) {
printf("%d\t", numbers[i]);
}
// 2. Operation: Multiply each element by factor
printf("\n\nEnter multiplication factor: ");
scanf("%d", &factor);
for (i = 0; i < 5; i++) {
numbers[i] = numbers[i] * factor;
}
// 3. Traversal Output Loop
printf("Updated numbers array:\n");
for (i = 0; i < 5; i++) {
printf("%d\t", numbers[i]);
}
printf("\n");
return 0;
}
Base Address Mapping
Execution begins. The name numbers holds the base address (e.g., 2000). The for loop counter i initializes to 0.
Sequential Pointer Advancement
Inside the scanf or assignment loop, the runtime CPU calculates Base_Addr + (i × 2) (for Turbo C) bytes to populating each sequential locker slot.
Loop Continuation and Offset Bound
The loop counter i increments, advancing the memory pointer by exactly 2 bytes per step. Loop terminates when i reaches SIZE, boundary logic ensures contiguous slots are used.
numbers[5] = 100; when size is 5) is illegal but compile-able; it corrupts neighbor memory slots and causes segmentation faults. Always structure loop conditions as i < SIZE.
- Structure: A single linear list of items using one subscript `arr[index]`.
- Data Type: Derived homogeneous type; items must be identical types.
- Indexing: Zero-based; range is
0toSize - 1. - Memory: Occupies continuous sequential bytes in RAM ( locker row).
- Loop Dependency:
forloops are essential for streamlined 1D array I/O processing.