C Notes
Master one-dimensional arrays in C with detailed examples covering declaration, initialization, traversal, input/output operations, and common array algorithms.
A one-dimensional array is the simplest form of an array — a linear sequence of elements stored in contiguous memory locations. Think of it like a row of numbered boxes, where each box holds a value and can be accessed instantly using its position number (index).
In most real-world C programs, 1D arrays are the backbone for handling collections of data — whether it's storing sensor readings, managing a list of scores, or buffering input characters.
Declaration and Memory Allocation
When you declare a 1D array, you're asking the compiler to reserve a continuous block of memory:
| numbers | numbers | numbers | numbers | numbers |
|---|---|---|---|---|
| [0] | [1] | [2] | [3] | [4] |
| garbage | garbage | garbage | garbage | garbage |
Notice that without initialization, local arrays contain unpredictable garbage values.
Initialization Techniques
At Declaration Time
Runtime Initialization Using Loops
arr[0] = 0 arr[1] = 1 arr[2] = 4 arr[3] = 9 arr[4] = 16 arr[5] = 25 arr[6] = 36 arr[7] = 49 arr[8] = 64 arr[9] = 81
Reading Array Elements from User
How many elements? 5 Enter 5 integers: Element 1: 45 Element 2: 12 Element 3: 78 Element 4: 34 Element 5: 56 You entered: 45 12 78 34 56
Array Traversal Patterns
Traversal means visiting each element of the array exactly once. Here are common patterns:
Forward Traversal
Reverse Traversal
Step Traversal (Every Other Element)
Common Operations on 1D Arrays
Finding the Sum and Average
Sum = 270 Average = 45.00
Finding Maximum and Minimum
Maximum: 89 at index 4 Minimum: 12 at index 2
Counting Occurrences of an Element
3 appears 4 times in the array
Copying One Array to Another
Since you cannot directly assign one array to another using =, you must copy element by element:
Source: 10 20 30 40 50 Dest: 10 20 30 40 50
Size Calculation Using sizeof
A very important technique — calculating array length at compile time:
Warning: This technique only works when the array is in the same scope as thesizeofcall. Once an array is passed to a function, it decays to a pointer, andsizeofreturns the pointer size instead.
Array Bounds and Safety
C trusts the programmer completely — it performs no bounds checking:
This is both a strength (performance) and a weakness (security vulnerabilities like buffer overflows).
Practical Example: Frequency Counter
Element | Frequency --------|---------- 1 | 2 2 | 2 3 | 3 4 | 2 5 | 1
Interview Questions
Q1: What is the time complexity of accessing an element in a 1D array?
O(1) — constant time. Since elements are stored contiguously and each has a fixed size, the address of any element can be computed directly using the formula: base_address + index × sizeof(element).
Q2: How do you find the size of an array without using sizeof?
You can use pointer arithmetic: int size = *(&arr + 1) - arr;. Here, &arr + 1 points to the memory location right after the last element of the array.
Q3: What's the difference between int arr[5] = {0} and memset(arr, 0, sizeof(arr))?
Both zero-out the array. The initializer syntax works only at declaration time, while memset can be used anywhere. However, memset sets bytes to 0, which works for integers but not for setting all elements to a non-zero value (except -1 due to two's complement representation).
Q4: Can you declare an array of size 0 in C?
In standard C, zero-length arrays are not allowed. However, GCC supports them as an extension, primarily used as flexible array members at the end of structures.
Summary
- A 1D array is a linear collection of same-type elements in contiguous memory
- Elements are accessed using zero-based indexing with O(1) time complexity
- Always track array size separately — C arrays don't store their own length
- Use
sizeof(arr)/sizeof(arr[0])to calculate length (only works in the declaring scope) - Common operations include traversal, searching, finding min/max, and copying
- C performs no bounds checking — buffer overflows are the programmer's responsibility
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for One Dimensional Arrays in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, arrays, one, dimensional, one dimensional arrays in c programming
Related C Programming Topics