-
Array bounds checking
Hi all,
I need to include array bounds checking in the programs i write.
With strings i use the following for bounds checking :
Code:
char s[100];
scanf("%100s",s);
How could I implement bounds checking on a integer array?
Code:
int a[10];
int i;
for(i=0;i<10;i++)
scanf("%d",&a[i]);
Here a user could enter several integers (>10) separated by whitespaces which would result in overflow. How can I check this?
-
your loop scans only 10 numbers - so where do you see an overflow?
-
With
Code:
int a[10];
int i;
for (i = 0; i < 10; i++)
scanf("%d", &a[i]);
It does not matter whether the user chooses to enter ten integers on one line or one on each line, there is no overflow problem.
However, for string reading, you should be taking the null character into account:
Code:
char word[100];
scanf("%99s", word);