possible use of uninitialized variable in array

 

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);
}
 

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.

 
Thanks, I am new to this and tbh the concept of arrays confuses me. I have a hard time grasping them. I changed the code to int candles[4] = {0}; and the errors went away, but is that correct?
 
Peter Zyler: but is that correct?

Yes, because you initialized it in the for loop.

Reason: