Reputation: 1774
It seems with arrays it's easy to get an off-by-one error:
short xar[2] = {};
for (int i = 0; i <= sizeof(xar)/sizeof(*xar); ++i) {
xar[i-1]=i*i;
printf("Element i = %d\n", xar[i]);
}
Element i = 0
Element i = 0
Element i = -9272
Is there a good way to check for the out-of-bounds case? Or how is something like this usually done (it seems writing out of bound array values would be super easy to do!)
Upvotes: 1
Views: 1326