Arrays are contiguous sequences of the same type. In most expressions, an array decays to a pointer to its first element.
Declaration and indexing
int nums[5] = {1, 2, 3, 4, 5};
int first = nums[0];
int len = sizeof(nums) / sizeof(nums[0]);
sizeof trick works for stack arrays, not pointers returned from functions.
Arrays and pointers
nums[i] is equivalent to *(nums + i). Bounds are not checked—accessing out of range is undefined behavior.
Important interview questions and answers
- Q: Array vs pointer?
A: Arrays have size information at definition; pointers are just addresses—decay loses length unless tracked separately. - Q: Can you assign arrays?
A: No—array names are not assignable; copy element by element or usememcpy.
Self-check
- How do you get the length of a stack array?
- What happens if you read nums[100] on a 5-element array?
Tip: Track array length separately when passing pointers—sizeof only works on the array at its definition site, not on decayed pointers.
Interview prep
- Array decay?
In most expressions an array name becomes a pointer to its first element—length information is lost unless tracked separately.
- Bounds checking?
C does not check array bounds at runtime—out-of-range access is undefined behavior.