我的代码编译并运行,但我仍然收到一条 lint 错误消息:
--- 模块:LunchMenu_main.c (C)
} lunch[LUNCHES] =
LunchMenu_main.c: 警告 956:(注意 -- 非 const、非易失性静态或外部变量“午餐”)
虽然演示了非常量静态和外部变量的使用,但使用它们有很多陷阱,除非没有其他合理的解决方案,否则应避免使用它们。有什么办法可以避免这些类型的变量,或者我需要这些变量来修复这个错误吗?这是我的代码:
struct Food
{
char *name;
int weight, calories;
} lunch[LUNCHES] =
{{(char *)"apple", 4, 100}, {(char *)"salad", 2, 80}};
int main(void)
{
int counter;
struct Food *foodPtr = &lunch[0];
printf("%-10s %-10s %-10s\n", "name", "weight", "calories");
for (counter = 0; counter < 2; counter++)
{
foodPtr = &lunch[counter];
printf("%-10s %-10d %-10d\n",
foodPtr->name, foodPtr->weight, foodPtr->calories);
}
return 0;
}