определить код:
#include <stdio.h>
#include <string.h>
typedef int count_t;
typedef struct ptid_t {
short index;
short ioffset;
unsigned char type;
unsigned char networkType;
} ptid_t;
typedef union ptid_lst {
count_t count;
ptid_t item[1];
} plist_t;
основной код:
int main(void) {
plist_t test;
memset(&test, 0x0, sizeof(plist_t));
test.count = 0xABCDABCD;
printf("%x\n", test.count);
printf("%x\n", test.item[0].index);
printf("%x\n", test.item[0].ioffset);
return 0;
}
консольный вывод:
abcdabcd
ffffabcd
ffffabcd
Я просто пытаюсь изменить struct first value 'count' но другие переменные изменены.
Значением изменения является 'count' в plist_t. но почему переменные index и ioffset изменены одновременно?
Из-за этой ситуации я пытаюсь получить адрес и результат переменной:
0x80479f4
0x80479f4
0x80479f6
Переменная 'count' и структура item [0] имеют одинаковый адрес. почему возникла такая ситуация?
В противоположном случае тоже самое.
int main(void) {
plist_t test;
memset(&test, 0x0, sizeof(plist_t));
test.item[0].index = 0xabcd;
test.item[0].ioffset = 0xabc0;
printf("%x\n", test.count);
printf("%x\n", test.item[0].index);
printf("%x\n", test.item[0].ioffset);
return 0;
}
console output:
abc0abcd
ffffabcd
ffffabc0