在.h文件中声明
struct food {
int ID;
QString name;
int num;
float price;
};
同时extern struct food food_infos[20];
在。c文件中对其进行初始化
struct food food_infos[] = { //注意前面必须要加入struct food
{1,"qingcai",0,8},
{2,"qiezi",0,12},
{3,"bocai",0,18},
{4,"xihongshi",0,12},
};
可以通过下面的语句获取到变量food_infos[]的长度实际长度
int z = sizeof(food_infos) / sizeof(food);
下面是参考代码:
#include<stdio.h> struct food { int Number; char *name; char *introduction; float price; }; extern food food_infos[20]; int main() { int ordered[100]={1,2}; int i,j,z; struct food food_infos[] = { {1,"baicai","keysdsadf ",12}, {2,"ruo","sdfdsafdsa",13}, }; z = sizeof(food_infos) / sizeof(food); printf("z= %d/n",z); for(i=0,j=0; i<2,j<z;i++,j++) if(ordered[i] == food_infos[j].Number) printf("%s/n",food_infos[j].name); getchar(); }