gpt4 book ai didi

似乎可以在结构内部传递结构,有什么想法吗?

转载 作者:行者123 更新时间:2023-11-30 19:21:04 24 4
gpt4 key购买 nike

每次我尝试初始化一个结构体以放置在名为“meal”的数组中,该数组包含一个结构体和一个整数时,都会出现以下错误:

..\meal_plan.c:92:5:警告:初始化器周围缺少大括号 [-Wmissing-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++;
}

最佳答案

您必须将 pfs 指向的内容复制到 fpi1.fs

food_plan_item fpi1 = {.qty = 1}; // designated initializer

memcpy(&fpi1.fs, pfs, sizeof(food_stuff));

或者您可以将 food_plan_itemfs 成员声明为指针,然后直接分配指针:

food_stuff *fs;

关于似乎可以在结构内部传递结构,有什么想法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20578091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com