struct packed_struct{
unsigned int f1 : 1; //一位的位域
unsigned int f2 : 1;
unsigned int f3 : 1;
unsigned int f4 : 1;
unsigned int type : 4;
unsigned int my_int : 9;
};
int main(void){
struct packed_struck pack;
pack.f1 = 1;
pack.f2 = 0;
pack.f3 = 1;
pack.f4 = 0;
pack.type = 7;
pack.my_int = 255;
printf("f1: %u\n", pack.f1);
printf("f2: %u\n", pack.f2);
printf("f3: %u\n", pack.f3);
printf("f4: %u\n", pack.f4);
printf("type: %u\n",pack.type);
printf("my_int: %u\n", pack.my_int);
return 0;
}
13 23 D:\zuojunyun\C code?疵?名1.c [Error] storage size of 'pack' isn't known
错误信息 "storage size of 'pack' isn't known" 表示编译器无法确定结构体 packed_struct 的大小。这通常是因为在程序中存在一个拼写错误。在你的代码中,错误是由于在 main 函数中尝试创建结构体 packed_struct 实例时拼写错误,写成了 packed_struck,应该是 packed_struct。所以你需要将以下代码行中的错误修正:
c
Copy code
struct packed_struck pack;
改为:
c
Copy code
struct packed_struct pack;
这样就能够正确创建结构体实例了。修复后的代码如下:
c
Copy code
struct packed_struct {
unsigned int f1 : 1;
unsigned int f2 : 1;
unsigned int f3 : 1;
unsigned int f4 : 1;
unsigned int type : 4;
unsigned int my_int : 9;
};
int main(void) {
struct packed_struct pack;
pack.f1 = 1;
pack.f2 = 0;
pack.f3 = 1;
pack.f4 = 0;
pack.type = 7;
pack.my_int = 255;
printf("f1: %u\n", pack.f1);
printf("f2: %u\n", pack.f2);
printf("f3: %u\n", pack.f3);
printf("f4: %u\n", pack.f4);
printf("type: %u\n", pack.type);
printf("my_int: %u\n", pack.my_int);
return 0;
}
现在代码应该能够正确编译和运行了。
6