gpt4 book ai didi

c - 在c结构数组中显示值

转载 作者:太空宇宙 更新时间:2023-11-04 07:16:50 25 4
gpt4 key购买 nike

我试图显示我的结构数组的值,编译器抛出以下错误:

athletes.c:17: error: expected ')' before '*' token

有人可以帮我解决问题吗?如果可能的话,请解释一下我做错了什么。

#include <stdio.h>
#include <string.h>
struct athlete {
char last_name[25];
char first_name [20];
int rank;
float salary;
};


int main (void) {
struct athlete players[] = {{"Lebron", "James",1,25}, {"Durant", "Kevin",3,20},{"Duncan","Tim",2,12}};
display_jock(players);

}

void display_jock(athlete *p) {
printf ("%10c%10c%10d%10.1f \n", p[0].last_name,p[0].first_name,p[0].rank, p[0].salary);
printf ("%10c%10c%10d%10.1f \n", p[1].last_name,p[1].first_name,p[1].rank, p[1].salary);
printf ("%10c%10c%10d%10.1f \n", p[2].last_name,p[2].first_name,p[2].rank, p[2].salary);
}

最佳答案

有几个小错误:

  • 您的代码不知道“athlete”类型,因此您的类 display_jock 应该使用参数 struct athlete 定义:void display_jock(struct athlete *p)
  • 您应该转发声明该函数:否则 main 不知道它(编辑:您也可以将 display_jock 函数移动到 main 函数的顶部)
  • 打印字符数组时,使用 printf 时应使用 %s 而不是 %c
  • 你的主函数应该返回一个int(因为它被声明为int main...)

这是你修复的代码:

#include <stdio.h>
#include <string.h>
struct athlete {
char last_name[25];
char first_name [20];
int rank;
float salary;
};

void display_jock(struct athlete *p);


int main (void) {
struct athlete players[] = {{"Lebron", "James",1,25}, {"Durant", "Kevin",3,20},{"Duncan","Tim",2,12}};
display_jock(players);
return 0;

}

void display_jock(struct athlete *p) {
printf ("%s%s%10d%10.1f \n", p[0].last_name,p[0].first_name,p[0].rank, p[0].salary);
printf ("%s%s%10d%10.1f \n", p[1].last_name,p[1].first_name,p[1].rank, p[1].salary);
printf ("%s%s%10d%10.1f \n", p[2].last_name,p[2].first_name,p[2].rank, p[2].salary);
}

关于c - 在c结构数组中显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23917119/

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