From: John N S Gill (jng at domain renre-europe.com)
Date: Wed 10 May 2000 - 17:57:40 IST
unless i am missing something when input is 10 then you are writing
to frequency[9] which is beyond the end of the array and so all bets
are off.
> gcc seems to do weird things to the following:
>
> int input, frequency[9];
>
> //frequency[] initialised to 0.
>
> do {
> scanf("%d", &input);
> if (input > 0 && input < 11) {
> //point 1
> (frequency[input-1])++;
> //point 2
> }
> } while (input);
>
> if i compile with -O2 or -O3 it works as i expect it to, but if i compile
> with "gcc -o test test.c" then:
>
> it works correctly for digits 0 thru 9, however if i enter 10 it goes
> wrong: at point 1 input == 10, at point 2 input == 11!
>
> seems like gcc is applying the increment to variable input,
> despite my bracketing of frequency[input-1]! which i don't
> want. why???
>
> (even without the brackets it should not increment input)
>
> --paulj.
>
> PS: is there anyway to initialise an array at declaration time, rather
> than iterate thru the array and set each element manually? eg something
> like: int array[9]=0;
>
>
>
> --
> Irish Linux Users' Group: ilug at domain linux.ie
> http://www.linux.ie/mailman/listinfo/ilug for (un)subscription information.
> List maintainer: listmaster at domain linux.ie
This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:06:05 GMT