Kjetil Eide Kjetil Eide - 2 months ago 5x
C Question

Initialize unsigned char array in c

I have the following code that initializes an array of unsigned chars:

unsigned char ultralightAtr[] = {0x3b,0x8f,0x80,0x01,0x80,0x4f,0x0c,0xa0,0x00,0x00,0x03,0x06,0x03,0x00,0x03,0x00,0x00,0x00,0x00,0x68};

When i inspect ultralightAtr in gdb I don't get the same values:

(gdb) x/20x ultralightAtr
0x7fffffffdd40: 0x70 0xdd 0xff 0xff 0xff 0x7f 0x00 0x00
0x7fffffffdd48: 0x70 0x19 0xbd 0xf7 0xff 0x7f 0x00 0x00
0x7fffffffdd50: 0x00 0xc7 0xfd 0xf7

Am I doing something wrong initializing the array or is it something wrong with the way I am printing the memory content in gdb?


Both the address and the contents of ultralightAtr indicate it is a local variable with automatic storage (aka on the stack). You probably examine its value with gdb before its initialization code is run in the function where it is defined. Did you just set a breakpoint at the function address and issued the x command there?

If this array is not modified in the function, you can make it static so it is initialized at load time instead of every time the function is entered.