possible use of uninitialized variable in array

Peter Zyler  

I'm hoping someone can help. This array was fine but all of a sudden I get this warning: possible use of uninitialized variable 'candles'. Not sure why the error.


int getPattern()
{
    if(Bars < 4) {
        return(0);
    }

    int candles[4];

    for(int i=0; i<4; i++) {
        candles[i]  = candleType(i+1);

        if(0 == candles[i]) {
            return(0);
        }
    }
   
    if(candles[0] == candles[1] && candles[1] != candles[2] && candles[2] == candles[3]) {
        return(candles[0]);
    }

    return(0);
}
Keith Watford  

You get the warning because you do not initialize the array.

The array elements are given values but within the for loop. The compiler cannot detect this so it issues the warning.

William Roeder  
Peter Zyler: but is that correct?

Yes, because you initialized it in the for loop.

Reason: