lundi 19 octobre 2020

Why would a variable not maintain its value outside of an if-statement?

I'm writing a piece of code that requires a change be made to an element in a structure. The code segment I'm displaying adds some numbers to an element of the struct and then changes the choice element of the struct to a new value.

if (itemList[itemCounter + 1].choice == SPACE_CONTROL_CHOICE || itemList[itemCounter + 1].choice == SYMBOL_CHOICE) {
   lexemeList[lexemeCounter].digit = 0;
   for(int i = digitCounter - 1; i >= 0; i--) {
       lexemeList[lexemeCounter].digit += tempInt[i];
           if (i != 0)
               lexemeList[lexemeCounter].digit *= 10;
   }
   digitCounter = 0;
   lexemeList[lexemeCounter].choice = DIGIT_CHOICE;
   printf("%d\n", lexemeList[lexemeCounter].choice);
   lexemeCounter++;
}

The problem that I am arriving at, though, is that the .choice value and the .digit value do not maintain the values that they are set as outside of this if-statement. These variables are not declared within the if-statement and I don't see any obvious error with my code. The variables revert to the values they were in before the if-statement executes. Why would this occur?

Aucun commentaire:

Enregistrer un commentaire