Mail Archives: djgpp/1997/08/28/22:48:24
At 08:12 8/24/1997 GMT, Mike4148 wrote:
>I've had a recurring problem with just about every type of program
>imaginable (from simple test progs, to games with Allegro, to file
>manipulation utilites). Sometimes, when I create an array and index the
>last element, I get a SIGSEGV and the program terminates. Here is a little
>bit of pointless example code to illustrate the situation :
>
>int lots_of_ints[4];
>lots_of_ints[4] = 1;
>
In the C language, arrays are indexed starting at zero. In your example,
lots_of_ints was an array of 4 ints, addressed as lots_of_ints[0] ..
lots_of_ints[3]. Your example tried to access the fifth element of a
four-element array, which is clearly illegal and deserves a SIGSEGV.
>int lots_of_other_ints[4] = {0, 1, 2, 3, 4};
>
>This snippet of code would get an error such as "array subscript out of
>bounds" (I'm not sure exactly what it was, this is the jist of it). I'm
>sure that no other part of my code is the problem. Any suggestions? Thanks
>in advance.
See above. You are initializing a four-element array with five elements. The
compiler is right to stop you.
Nate Eldredge
eldredge AT ap DOT net
- Raw text -