首页 新闻 会员 周边

devc++ 13 13 23 [Error] storage size of 'pack' isn't known

0
[已解决问题] 解决于 2023-11-09 15:11

include <stdio.h>

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

王家兴的主页 王家兴 | 菜鸟二级 | 园豆:210
提问于:2023-10-16 16:12
< >
分享
最佳答案
1

错误信息 "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

include <stdio.h>

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;

}
现在代码应该能够正确编译和运行了。

奖励园豆:5
Technologyforgood | 大侠五级 |园豆:5528 | 2023-10-16 19:43

6

王家兴 | 园豆:210 (菜鸟二级) | 2023-10-16 19:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册