我一直在尝试打印我创建的结构体的成员,但是有一些声明错误显示我的结构体未声明。我有一个单独的函数来打印结构体的成员。我不知道如何调试它…请帮助我有错误,如game1-未声明(第一次使用在这个函数)和预期=,;{token
之前的Asm或属性#include <stdio.h>
#include <stdlib.h>
struct video_game
{
char *name, *genre, *developer, *platformer, *app_purchase;
int release_year, age_limit;
float price;
};
void print_video_game_details(struct video_game* s)
{
printf("nTitle: %sn", s->name);
printf("Genre: %sn", s->genre);
printf("Developer: %sn", s->developer);
printf("Year of Release: %dn", s->release_year);
printf("Lower Age Limit: %dn", s->age_limit);
printf("Price: $%fn", s->price);
printf("In-app Purchase: %sn", s->app_purchase);
}
int main(int agrc, char* agrv[])
{
struct video_game game1
{
game1.name = "Candy Crush Saga";
game1.genre = "Match-Three Puzzle";
game1.developer = "King";
game1.release_year = 2012;
game1.platform = "Android, iOS, Windows Phone";
game1.age_limit = 7;
game1.price = 0.00;
game1.app_purchase = "Yes";
};
struct video_game game2
{
game2.name = "Halo 4";
game2.genre = "First Person Shooter";
game2.developer = "343 Industries";
game2.release_year = 2014;
game2.platform = "Xbox 360, Xbox One";
game2.age_limit = 16;
game2.price = 69.95;
game2.app_purchase = "No";
};
struct video_game game1
{
game3.name = "Uncharted 2: Among Thieves";
game3.genre = "Action adventure RPG";
game3.developer = "Naughty Dog";
game3.release_year = 2012;
game3.platform = "PS3";
game3.age_limit = 16;
game3.price = 30.00;
game3.app_purchase = "No";
};
print_video_game_details(&game1);
print_video_game_details(&game2);
print_video_game_details(&game3);
return 0;
}
您的实例创建(game1
, game2
和game3
)不是C语言,它们使用一些虚构的语法。
应该像
struct video_game game1 = {
.name = "Candy Crush Saga",
/* ... */
};
您需要定义三个类型为struct video_game
的变量,而<type> <name> [= <initializer>]
(大致)是c中变量的定义方式。
如果你没有C99,它必须是:
struct video_game game1 = {
"Candy Crush Saga",
"Match-Three Puzzle",
"King",
"Android, iOS, Windows Phone",
"Yes",
2012,
7,
0.00
};
注意事项,你似乎忽略了:
- 初始化器中没有字段名,只有值。
- 顺序必须与
struct
声明时完全一致;首先是五个字符串,然后是两个整数,然后是一个浮点数。 - 值之间用逗号分隔,而不是分号。