Arrays
Challenge Gallery
Quick Reference
Declaration patterns:
| Pattern | What it does |
|---|---|
int arr[5]; |
5 ints, uninitialized (garbage) |
int arr[5] = {0}; |
5 ints, all zero |
int arr[] = {1,2,3}; |
3 ints, compiler counts |
int arr[5] = {1,2}; |
{1, 2, 0, 0, 0} |
Size calculation (local scope only):
int arr[100];
int count = sizeof(arr) / sizeof(arr[0]); // 100
// Does NOT work inside functions that receive the array!
Passing to functions:
void process(const int arr[], int size); // Read-only
void modify(int arr[], int size); // Can modify
Common Pitfalls
- No bounds checking —
arr[1000]on a size-10 array compiles and runs. Silent corruption or crash. - Uninitialized arrays — Local arrays contain garbage. Use
= {0}or set values before reading. - sizeof in functions — Returns pointer size (8), not array size. Always pass size separately.
- No .length — C arrays don’t know their own size. Track it yourself.
- Can’t assign arrays —
arr1 = arr2doesn’t compile. Use a loop ormemcpy. - Buffer overflows — #1 security vulnerability in C. Always check indices against size.