鱼C论坛

 找回密码
 立即注册
查看: 4214|回复: 2

请分析下面代码

[复制链接]
发表于 2012-8-1 21:18:14 | 显示全部楼层 |阅读模式
3鱼币
#include<stdio.h>
#define NULL 0
struct student{
   long num;
   float score;
   struct student*next;
};
void main()
{
        struct student a,b,c,*head,*p;
        a.num=1;a.score=90.0;
        b.num=2;b.score=95.0;
        c.num=3;c.score=70.0;
        head=&a;
        a.next=&b;
        b.next=&c;
        c.next=NULL;
        p=head;
        do{
                printf("%d &5.1f\n",p->num,p->score);
                p=p->next;
        }while(p!=NULL);
}

知道输出是结构共同体,但部分代码作用忘了!!尤其上面的printf("%d &5.1f\n",p->num,p->score);请说明下吧!!

小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-8-1 21:18:15 | 显示全部楼层
  1. #include<stdio.h>
  2. //#define NULL 0                这里不需要你重复定义 NULL 为0 了
  3. struct student{
  4.     long num;
  5.     float score;
  6.     struct student*next;
  7. };
  8. void main()
  9. {
  10.         struct student a,b,c,*head,*p;
  11.         a.num=1;a.score=90.0;
  12.         b.num=2;b.score=95.0;
  13.         c.num=3;c.score=70.0;
  14.         head=&a;
  15.         a.next=&b;
  16.         b.next=&c;
  17.         c.next=NULL;
  18.         p=head;
  19.         do{
  20.                 printf("%d %5.1f\n",p->num,p->score);/*printf("%d &5.1f\n",p->num,p->score);*/        //你的printf 里面的 &5.1f是什么意思? 应该是 %5.1f 吧 实数的输出格式 你用结构体指针来索引输出 所以用p->num 和p-score 来输出 你也可以写成(*p).num 和 (*p).score 一样的
  21.                 p=p->next;                //指向下一个结点
  22.         }while(p!=NULL);
  23. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-8-1 22:13:34 | 显示全部楼层
呵呵。真不知道你是怎么写的。别的都对。就是一个小错误。printf("%d %5.1f\n",p->num,p->score);
按理来说。不会的啊。指针的知识掌握了。不应该在输出的格式上犯这个错误啊。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-12 04:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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