找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

关于C语言结构体显示的问题

查看数: 3599 | 评论数: 4 | 收藏 0
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2020-10-22 22:07

正文摘要:

想把结构体用在单片机上 可是发现我不会,我就用turboc写了个试试 结果根本没有用 搞得焦头烂额 希望大家帮帮我 如图 绿色荧光笔为我想显示的代码,结果显示null 我晕倒!

回复

ID:833190 发表于 2020-10-24 15:27
本帖最后由 i_could_have 于 2020-10-24 15:39 编辑
  1. #define _CRT_SECURE_NO_WARNINGS

  2. #include <stdio.h>
  3. struct books{
  4.     char name[20];
  5.     char author[20];
  6.     float price[20];
  7. };
  8. int i = 1;
  9. int main(void)
  10. {
  11.     struct books library;
  12.     printf("name of the book?\n");
  13.     scanf_s("%s", library.name,20);
  14.     printf("author of the book?\n");
  15.     scanf_s("%s", library.author,20);
  16.     printf("price of the book?\n");
  17.     scanf("%f", &library.price[i-1]);
  18.     int answer = 0;
  19.     if (scanf("%d", &answer) == 1)
  20.     {
  21.         printf("You got %d books\n", i);
  22.         for (; i > 0; i--)
  23.         {
  24.             printf("book number:%d\n", i);
  25.             printf("name:%s\n", library.name);
  26.             printf("author:%s\n", library.author);
  27.             printf("price:%f\n", library.price[i-1]);
  28.         }
  29.         return 0;
  30.     }
  31.     else i++;
  32.     return 1;
  33. }

  34. 你把字符数组键盘输入那块的&符号去掉。
复制代码
ID:833190 发表于 2020-10-24 14:47
  1. #define _CRT_SECURE_NO_WARNINGS

  2. #include <stdio.h>
  3. struct books{
  4.     char name[20];
  5.     char author[20];
  6.     float price[20];
  7. };
  8. int i = 1;
  9. int main(void)
  10. {
  11.     struct books library;
  12.     printf("name of the book?\n");
  13.     scanf_s("%s", library.name,20);
  14.     printf("author of the book?\n");
  15.     scanf_s("%s", library.author,20);
  16.     printf("price of the book?\n");
  17.     scanf("%f", &library.price[i-1]);
  18.     int answer = 0;
  19.     if (scanf("%d", &answer) == 1)
  20.     {
  21.         printf("You got %d books\n", i);
  22.         for (; i > 0; i--)
  23.         {
  24.             printf("book number:%d\n", i);
  25.             printf("name:%s\n", library.name);
  26.             printf("author:%s\n", library.author);
  27.             printf("price:%f\n", library.price[i-1]);
  28.         }
  29.         return 0;
  30.     }
  31.     else i++;
  32.     return 1;
  33. }
  34. 字符数组不用&,字符数组的名字就代表的是首地址
复制代码
ID:101277 发表于 2020-10-23 06:54
你连数组操作这块都还没搞清楚。
ID:690448 发表于 2020-10-22 22:07
我猜楼主是想实现以下功能:

用循环来输入书本的信息,但是预先不知道要输入多少本,当answer  == 1 时就停止输入,
然后打印书本的总数,并且输出每一本的信息。


若真是这样,难度有点大……

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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