每当我尝试初始化一个结构放在一个名为“meal”的数组中,该数组包含一个结构和一个整数时,它会给我以下错误:
.. \ meal_plan.c:92:5:警告:初始化器周围缺少大括号[-Wateing-braces]
.. \ meal_plan.c:92:5:警告:(接近初始化'fpi1.fs')[-Wmissing-braces]
.. \ meal_plan.c:92:5:警告:初始化从指针生成整数而没有强制转换[默认启用]
.. \ meal_plan.c:92:5:警告:(接近初始化'fpi1.fs.name [0]')[默认启用]
我不能为我的生活弄清楚什么是错的。我真的很感激有人对我失踪的看法。
struct food_stuff
{
char name [30];
int foodtype;
int calories;
int price;
};
typedef struct food_stuff food_stuff;
struct food_plan_item
{
food_stuff fs;
int qty;
};
typedef struct food_plan_item food_plan_item;
food_plan_item饭[50];
void choose_food_item()
{
puts("Please select one of the available foods:");
puts("-----------------------------------------");
for(int i = 0;i <f_cnt;i++)
{
printf("\t(%d.) %s\n",i, (foodArray+i)->name);
}
puts("-----------------------------------------");
fflush(stdout);
char itemSelection [3];
int chosenitem;
gets(itemSelection);
sscanf(itemSelection,"%d",&chosenitem);
food_stuff *pfs;
pfs = (foodArray+chosenitem);
food_plan_item fpi1 = {pfs,1}; **(LINE 92)**
add_food_plan_item(&fpi1);
}
void add_food_plan_item(food_plan_item *p_fpi)
{
meal[fpi_count] = *p_fpi;
fpi_count++;
}
答案 0 :(得分:1)
您必须从pfs
点复制到fpi1.fs
;
food_plan_item fpi1 = {.qty = 1}; // designated initializer
memcpy(&fpi1.fs, pfs, sizeof(food_stuff));
或者您可以将fs
的{{1}}成员声明为指针,然后直接指定指针:
food_plan_item