Q
quemix
Hi all:
I have defined a structure (bit field) like this:
struct bit_test {
int a:4,
b:8,
c:20;
};
struct bit_test test;
To know the real memory storage of "test", I print every byte of it.
But surprisingly, the result is wrong: 56 ffffff80 0 0.
And when I use the same code to print an interger, it works well.
Here is my code and its output, can you give me some advice? Thank
you.
output of my code:
Print bit-field structure:
56 ffffff80 0 0
Print interger:
78 56 34 12
Code:
#include <stdio.h>
struct bit_test {
int a:4,
b:8,
c:20;
};
int main(void)
{
int i, testi;
struct bit_test test;
char *p;
test.a = 6;
test.b = 5;
test.c = 8;
p = (char *)&test;
printf("Print bit-field structure:\n");
for (i = 0; i < sizeof(struct bit_test); i++) {
printf("%x ", p);
}
printf("\n");
testi = 0x12345678;
p = (char *)&testi;
printf("Print interger:\n");
for (i = 0; i < sizeof(int); i++) {
printf("%x ", p);
}
printf("\r\n");
return 0;
}
I have defined a structure (bit field) like this:
struct bit_test {
int a:4,
b:8,
c:20;
};
struct bit_test test;
To know the real memory storage of "test", I print every byte of it.
But surprisingly, the result is wrong: 56 ffffff80 0 0.
And when I use the same code to print an interger, it works well.
Here is my code and its output, can you give me some advice? Thank
you.
output of my code:
Print bit-field structure:
56 ffffff80 0 0
Print interger:
78 56 34 12
Code:
#include <stdio.h>
struct bit_test {
int a:4,
b:8,
c:20;
};
int main(void)
{
int i, testi;
struct bit_test test;
char *p;
test.a = 6;
test.b = 5;
test.c = 8;
p = (char *)&test;
printf("Print bit-field structure:\n");
for (i = 0; i < sizeof(struct bit_test); i++) {
printf("%x ", p);
}
printf("\n");
testi = 0x12345678;
p = (char *)&testi;
printf("Print interger:\n");
for (i = 0; i < sizeof(int); i++) {
printf("%x ", p);
}
printf("\r\n");
return 0;
}