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

时间:2015-06-01 09:12:21

标签: c debugging pointers struct c89

我一直在尝试打印我创建的结构的成员,但是有一些声明错误显示我的结构未声明。我有一个单独的函数来打印结构的成员。我不知道如何调试它...请帮忙 我有错误,如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;
}  
  
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

您的实例创建(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的订单完全相同;前五个字符串,然后是两个整数,然后是一个浮点数。
  • 值以逗号分隔,而不是分号。