鱼C论坛

 找回密码
 立即注册
查看: 4356|回复: 7

关于结构体读取输出 出错

[复制链接]
发表于 2021-5-18 23:23:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
为什么这段不能正常读取和输出结构体当中的内容呢
  1. #include<stdio.h>
  2. #include<string.h>
  3. struct Student
  4. {
  5.         char num;
  6.         char name[20];
  7.         float score;
  8. }student[3];

  9. int main()
  10. {
  11. char max, i;

  12. for(i = 0; i < 3; i++)
  13. {
  14.         scanf("%c%s%f",&student[i].num, student[i].name, &student[i].score);
  15. }

  16. for(i = 0; i < 3; i++)
  17. {
  18.         printf("%c %s %.2f\n",student[i].num, student[i].name,student[i].score);
  19. }

  20. printf("The higer score is:\n");

  21. return 0;
  22. }       
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-18 23:25:33 | 显示全部楼层
scanf("%c%s%f",&student[i].num, student[i].name, &student[i].score);
"%c%s%f" // 分割符呢?
student[i].name // &呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-19 09:09:36 From FishC Mobile | 显示全部楼层
ba21 发表于 2021-5-18 23:25
scanf("%c%s%f",&student.num, student.name, &student.score);
"%c%s%f" // 分割符呢?
student.name / ...

数组不是不用取址吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-19 09:24:05 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<string.h>
  3. struct Student
  4. {
  5.     char num;
  6.     char name[20];
  7.     float score;
  8. }student[3];

  9. int main()
  10. {
  11.     //char max, i;
  12.     int max, i;

  13.     for(i = 0; i < 3; i++)
  14.     {
  15.         scanf("%c%s%f",&student[i].num, student[i].name, &student[i].score);
  16.         getchar();
  17.     }

  18.     for(i = 0; i < 3; i++)
  19.     {
  20.         printf("%c %s %.2f\n",student[i].num, student[i].name,student[i].score);
  21.     }

  22.     printf("The higer score is:\n");

  23.     return 0;
  24. }        
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-19 09:49:13 From FishC Mobile | 显示全部楼层
人造人 发表于 2021-5-19 09:24

请问这里为什么要加一个getchar呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-19 10:17:48 | 显示全部楼层
Mr丶张 发表于 2021-5-19 09:49
请问这里为什么要加一个getchar呢

去掉在输入流中最后那个多余的 '\n'
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-25 09:08:57 From FishC Mobile | 显示全部楼层
人造人 发表于 2021-5-19 09:24

什么情况下要用getchar()??
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-25 09:48:31 | 显示全部楼层
最强废铁h 发表于 2021-5-25 09:08
什么情况下要用getchar()??

当 scanf 最后一个接收的不是 %c 的时候
  1. #include <stdio.h>

  2. int main(void) {
  3.     int a, b, c;
  4.     char ch;
  5.     scanf("%d%d%d", &a, &b, &c);
  6.     getchar();
  7.     scanf("%c", &ch);
  8.     printf("%d, %d, %d, '%c'\n", a, b, c, ch);
  9.     return 0;
  10. }

  11. #include <stdio.h>

  12. int main(void) {
  13.     int a, b, c;
  14.     char ch1, ch2;
  15.     scanf("%d%d%d%c", &a, &b, &c, &ch1);
  16.     scanf("%c", &ch2);
  17.     printf("0x%x, 0x%x\n", ch1, ch2);
  18.     printf("%d, %d, %d\n", a, b, c);
  19.     return 0;
  20. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-3 12:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表