On Sat, 9 Jan 1999, Richard Ivanowich wrote:

> Hello all,
> 
> Now i know arrays do no bound checking.  How would i make it do bound
> checking?
> 
> ie.  int array[10];
> 
> for(i=0;i<=11;i++) {
>   array[i]=i;
> }

Simple rule: array[n] has n elements, indexing starts with 
0 and ends with n-1. 

One (potentially slow) way to do bounds checking with
automatic or static arrays with a constant number of 
elements is:

int array[10]
int i;
for(i = 0; i < sizeof(array) / sizeof(int); i++)
        array[i]=i;

But that is a real suckful way to go about it, much better 
would be to use a macro.

#define ARRAY_SIZE 10
int array[ARRAY_SIZE];

for(i = 0; i < ARRAY_SIZE; i++)
        array[i]=i;


Does this answer your question ?


Reply via email to