array index out of bounds [SOLVED]

Started by riseryn, Fri 14/12/2007 03:25:18

Previous topic - Next topic

riseryn

Hi
after wandering through the forum for one hour without finding a solution i'm completely lost.

the array ball is part of a structure and there is no problem with it.
it goes from ball[0] to ball[4]

int randi = Random(4);
int randj = Random(2)+1;
int k=0;
while(k<5)
{
if(k == randi){ ball[k].weight = randj;}

else{ball[k].weight =1;   }
k++;
}

when running, this code give me:
Error:  Array index out of bounds (index:4 bounds:0..3)

I dont understand why :(
maybe its very simple :)

Any suggestions?

Thanks

Gilbert

If you defind the array as something like:
int ball[4];
its index actually runs from 0 to 3.

If you want the index to run from 0 to 4, you need:
int ball[5];

riseryn

#2
thanks for your quick response

it works

SMF spam blocked by CleanTalk