Linked Questions

Popular Questions

Sanity check on definitions from arrays

Asked by At

I'm hunting for the cause of a bug and think I may have found it if my understanding of using #define is correct.

As i understand it, once you define something, its set that way, even if the define is filled with the value of a variable, it sticks with the value at the time of compile and would not change if the variable changed later? So this code:

int values[] = {5,6,7,8};
int x = 0;
#define DEF_VALUE values[x]

DEF_VALUE would be 5, as 5 is the value of position 0 in the values array.

But if I was to do this:

int values[] = {5,6,7,8};
int x = 0;
#define DEF_VALUE values[x]
x = 2;

DEF_VALUE would still be 5 as that was the output of values[x] at the time that #define was called, so changing the value of x later to 2 would not cause a change in DEF_VALUE to 7 (the value of position 2 in the values array).

Is my logic here correct?

If so I think I've found my bug as the original programmer of this code was relying on updating x to update the value of DEF_VALUE later in the code for a location address for selections.

Related Questions