使用函数打印结构
Using functions to print structures
声明一个描述单个视频游戏的结构。
电子游戏有标题、类型、平台、开发商、发行年份、年龄下限、价格和
他们是否支持 in-app 购买。您将需要为每个选择适当的数据类型
要存储在结构中的一条信息。
- 命名结构:Video_Game.
- 在main函数本地声明三个视频游戏结构体变量,分别叫game1,game2
和 game3.
- 将上述 Candy Crush Saga(King,2012)示例的详细信息分配给 game1 的成员。
- 将上述 Halo 4(343 Industries,2014)示例的详细信息分配给 game2 的成员。
- 分配给game3的成员,你最喜欢的游戏的详细信息......如果你不玩游戏,
检查你的 smart phone...肯定你在那里玩了一些东西...如果没有,请检查相关的应用程序商店
排行榜并找到一款可能成为您新宠的游戏!
- 接下来,声明一个名为 print_video_game_details() 的函数,它接受一个参数
那是指向先前声明的视频游戏结构的指针。在这个函数中,打印出的细节
游戏传递给函数,采用上面显示的 Candy Crush Saga 和 Halo 4 样式
示例。
- 接下来,从 main 中调用三次 print_video_game_details 函数,传入
game1,game2,game3的地址,成员全部设置好后
到目前为止我的代码:
#include <stdio.h>
#include <string.h>
struct video_game
{
char* title;
char* genre;
char* developer;
int year;
char* platform;
int lower_age;
float price;
char* inapp_purchase;
}game1, game2;
void print_video_game_details()
{
for(int i =1; i<=3; i++)
{
printf("Title: %s", game[i].title); // game[i] is showing an error "undeclared"
printf("Genre: %s", game[i].genre);
printf("Developer: %s", game[i].developer);
printf("year of release: %d", game[i].year);
printf("platform: %s", game[i].platform);
printf("lower age: %d", game[i].lower age);
printf("price: %f", game[i].price); //is showing an error "incompatible"
printf("inapp_purchase: %s", game[i].inapp_purchase);
}
}
int main(void)
{
game1.title = "Candy crush saga";
game1.genre = "Match-Three Puzzle";
game1.developer = "King";
game1.year = "2012";
game1.platform = "Android, ios, Windows Phone";
game1.lower_age = "7";
game1.price = "[=10=].00";
game1.inapp_purchase = "yes";
print_video_game_details();
}
我无法打印出结构,因为它无法编译。
错误:
prog.c: In function 'print_video_game_details':
prog.c:27:33: error: 'struct video_game' has no member named 'lower'
printf("lower age: %d", game[i].lower age);
^
prog.c:27:40: error: expected ')' before 'age'
printf("lower age: %d", game[i].lower age);
^
prog.c: In function 'main':
prog.c:39:15: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
game[0].year = "2012";
^
prog.c:41:20: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
game[0].lower_age = "7";
^
prog.c:42:16: error: incompatible types when assigning to type 'float' from type 'char *'
game[0].price = "[=11=].00";
^
您的代码存在的问题:
- 在行中:
printf("lower age: %d", game[i].lower age);
,lower age
应该是 lower_age
。
- 如果您打算在
print_video_game_details()
中使用 for 循环,您应该定义一个类似 struct video_game game[1];
的数组
- For 循环限制应从
i = 0
开始并在 i < [however many games there are]
时结束
- Main 应该定义
game[0]
、game[1]
等,而不是 game1
、game2
等
game[0].year
是一个整数,所以不要将 2012
放在引号中
game[0].price
是一个浮点数,所以去掉引号并去掉美元符号。
这是我们在评论中讨论过的代码。它适用于一个视频游戏。您可以增加 video_game game[x]
数量和 for 循环以支持多个视频游戏。
#include <stdio.h>
#include <string.h>
struct video_game
{
char* title;
char* genre;
char* developer;
int year;
char* platform;
int lower_age;
float price;
char* inapp_purchase;
}game1, game2;
struct video_game game[1];
void print_video_game_details()
{
int i;
for(i = 0; i < 1; i++)
{
printf("Title: %s\n", game[i].title);
printf("Genre: %s\n", game[i].genre);
printf("Developer: %s\n", game[i].developer);
printf("year of release: %d\n", game[i].year);
printf("platform: %s\n", game[i].platform);
printf("lower age: %d\n", game[i].lower_age);
printf("price: %f\n", game[i].price);
printf("inapp_purchase: %s\n", game[i].inapp_purchase);
}
}
int main(int argc, char* argv[])
{
game[0].title = "Candy crush saga";
game[0].genre = "Match-Three Puzzle";
game[0].developer = "King";
game[0].year = 2012;
game[0].platform = "Android, ios, Windows Phone";
game[0].lower_age = 7;
game[0].price = 0.0;
game[0].inapp_purchase = "yes";
print_video_game_details();
}
声明一个描述单个视频游戏的结构。 电子游戏有标题、类型、平台、开发商、发行年份、年龄下限、价格和 他们是否支持 in-app 购买。您将需要为每个选择适当的数据类型 要存储在结构中的一条信息。
- 命名结构:Video_Game.
- 在main函数本地声明三个视频游戏结构体变量,分别叫game1,game2 和 game3.
- 将上述 Candy Crush Saga(King,2012)示例的详细信息分配给 game1 的成员。
- 将上述 Halo 4(343 Industries,2014)示例的详细信息分配给 game2 的成员。
- 分配给game3的成员,你最喜欢的游戏的详细信息......如果你不玩游戏, 检查你的 smart phone...肯定你在那里玩了一些东西...如果没有,请检查相关的应用程序商店 排行榜并找到一款可能成为您新宠的游戏!
- 接下来,声明一个名为 print_video_game_details() 的函数,它接受一个参数 那是指向先前声明的视频游戏结构的指针。在这个函数中,打印出的细节 游戏传递给函数,采用上面显示的 Candy Crush Saga 和 Halo 4 样式 示例。
- 接下来,从 main 中调用三次 print_video_game_details 函数,传入 game1,game2,game3的地址,成员全部设置好后
到目前为止我的代码:
#include <stdio.h>
#include <string.h>
struct video_game
{
char* title;
char* genre;
char* developer;
int year;
char* platform;
int lower_age;
float price;
char* inapp_purchase;
}game1, game2;
void print_video_game_details()
{
for(int i =1; i<=3; i++)
{
printf("Title: %s", game[i].title); // game[i] is showing an error "undeclared"
printf("Genre: %s", game[i].genre);
printf("Developer: %s", game[i].developer);
printf("year of release: %d", game[i].year);
printf("platform: %s", game[i].platform);
printf("lower age: %d", game[i].lower age);
printf("price: %f", game[i].price); //is showing an error "incompatible"
printf("inapp_purchase: %s", game[i].inapp_purchase);
}
}
int main(void)
{
game1.title = "Candy crush saga";
game1.genre = "Match-Three Puzzle";
game1.developer = "King";
game1.year = "2012";
game1.platform = "Android, ios, Windows Phone";
game1.lower_age = "7";
game1.price = "[=10=].00";
game1.inapp_purchase = "yes";
print_video_game_details();
}
我无法打印出结构,因为它无法编译。
错误:
prog.c: In function 'print_video_game_details':
prog.c:27:33: error: 'struct video_game' has no member named 'lower'
printf("lower age: %d", game[i].lower age);
^
prog.c:27:40: error: expected ')' before 'age'
printf("lower age: %d", game[i].lower age);
^
prog.c: In function 'main':
prog.c:39:15: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
game[0].year = "2012";
^
prog.c:41:20: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
game[0].lower_age = "7";
^
prog.c:42:16: error: incompatible types when assigning to type 'float' from type 'char *'
game[0].price = "[=11=].00";
^
您的代码存在的问题:
- 在行中:
printf("lower age: %d", game[i].lower age);
,lower age
应该是lower_age
。 - 如果您打算在
print_video_game_details()
中使用 for 循环,您应该定义一个类似 - For 循环限制应从
i = 0
开始并在i < [however many games there are]
时结束
- Main 应该定义
game[0]
、game[1]
等,而不是game1
、game2
等 game[0].year
是一个整数,所以不要将2012
放在引号中game[0].price
是一个浮点数,所以去掉引号并去掉美元符号。
struct video_game game[1];
的数组
这是我们在评论中讨论过的代码。它适用于一个视频游戏。您可以增加 video_game game[x]
数量和 for 循环以支持多个视频游戏。
#include <stdio.h>
#include <string.h>
struct video_game
{
char* title;
char* genre;
char* developer;
int year;
char* platform;
int lower_age;
float price;
char* inapp_purchase;
}game1, game2;
struct video_game game[1];
void print_video_game_details()
{
int i;
for(i = 0; i < 1; i++)
{
printf("Title: %s\n", game[i].title);
printf("Genre: %s\n", game[i].genre);
printf("Developer: %s\n", game[i].developer);
printf("year of release: %d\n", game[i].year);
printf("platform: %s\n", game[i].platform);
printf("lower age: %d\n", game[i].lower_age);
printf("price: %f\n", game[i].price);
printf("inapp_purchase: %s\n", game[i].inapp_purchase);
}
}
int main(int argc, char* argv[])
{
game[0].title = "Candy crush saga";
game[0].genre = "Match-Three Puzzle";
game[0].developer = "King";
game[0].year = 2012;
game[0].platform = "Android, ios, Windows Phone";
game[0].lower_age = 7;
game[0].price = 0.0;
game[0].inapp_purchase = "yes";
print_video_game_details();
}