gpt4 book ai didi

c - 如何打印出结构体的成员,奇怪的错误?

转载 作者:行者123 更新时间:2023-11-30 18:54:16 24 4
gpt4 key购买 nike

我一直在尝试打印我创建的结构的成员,但是有一些声明错误显示我的结构未声明。我有一个单独的函数来打印结构的成员。我不知道如何调试它......请帮忙我有错误,例如 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: %s\n", s->name);
printf("Genre: %s\n", s->genre);
printf("Developer: %s\n", s->developer);
printf("Year of Release: %d\n", s->release_year);
printf("Lower Age Limit: %d\n", s->age_limit);
printf("Price: $%f\n", s->price);
printf("In-app Purchase: %s\n", 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;
}

最佳答案

您的实例创建( game1game2game3 )不是 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 时的顺序完全相同被宣布;前五个字符串,然后是两个整数,然后是一个 float 。
  • 值之间用逗号分隔,而不是分号。

关于c - 如何打印出结构体的成员,奇怪的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30569687/

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