Definition
📌 Arrays
An array stores multiple values of the same type in one contiguous block of memory, accessed using an index starting at 0.The size of a C array is fixed once it's declared — it can't grow or shrink at runtime..
Syntax
syntax
type arrayName[size];
type arrayName[size] = {value1, value2, ...};Example
c
#include <stdio.h>
int main() {
int scores[5] = {90, 85, 70, 60, 95};
for (int i = 0; i < 5; i++) {
printf("%d ", scores[i]);
}
printf("\n");
return 0;
}Output
output90 85 70 60 95
Important Points
- Array indices start at
0, not1. - An array's name in an expression "decays" into a pointer to its first element.
- Use
sizeof(arr) / sizeof(arr[0])to compute the number of elements. - Arrays are stored as a contiguous block of memory, which is why indexing is fast.
Common Mistakes
- Accessing index
sizeinstead ofsize - 1for the last element (off-by-one). - Reading/writing past the array's declared size — C won't warn you, it will just corrupt memory.
- Trying to resize a fixed-size array at runtime.
Interview Tip
🎯 Before your interview
Know that arrays and pointers are closely related in C:
arr[i] is really shorthand for *(arr + i).
