Reputation: 153
This is a stupid question, but I can't work it out and it's beginning to irritate me!
I have the following (reg is a global):
#define CS 4
#define DS 5
unsigned char reg[6] = {0, 0, 0, 0, 0, 0x10};
Now, I would expect reg[DS] to access element 5 of array reg (0x10). However, when debugging (VC2010), Visual Studio is claiming that DS is zero and the first element is accessed. Is it me, or is Visual Studio being stupid?
Upvotes: 1
Views: 1626
Reputation: 500495
Your understanding that reg[DS]
refers to the fifth (that is, the last) element of reg
is correct.
If you think that a different element might be getting accessed, I'd make sure that you're not redefining DS
to a different value else by accident, and that DS
isn't clashing with a symbol defined by one of the headers that you're including.
Note that DS
does not exist at run time. Even the compiler does not see get to see DS
, since it will have been replaced with 5
by the preprocessor. This is what could be tripping up the debugger.
Upvotes: 1
Reputation: 206546
Yes.
reg[DS] == 0x10
Unless you have redefined the macro DS
somewhere else in the code.
Macros are evaluated at pre-compilation. So if you defined DS
as 5
then what goes to the compiler for compilation is reg[5]
.
Also, You cannot check a macro value at run-time because they do not exist at run-time, they are already substituted with whatever value they are defined as.
Upvotes: 5