Array Index Out of Bounds
semantic
warning: array index 5 is past the end of the array
Description
Accessing an array element with an index that is outside the array's bounds.
Common Causes
- Using an index that is negative
- Using an index that is greater than or equal to the array's size
- Off-by-one errors in loop conditions
Example of Error
Error Code
int main() { int arr[5] = {1, 2, 3, 4, 5}; printf("%d\n", arr[5]); // Index out of bounds return 0; }
Solution
Ensure array indices are within the valid range (0 to size-1).
Corrected Code
int main() { int arr[5] = {1, 2, 3, 4, 5}; printf("%d\n", arr[4]); // Last valid index is 4 return 0; }
Additional Tips
- Remember that array indices start at 0
- Use array size - 1 as the maximum index
- Consider using bounds checking in critical code