一道C语言程序题目,求解答~~

这是程序的主体:structstu{charname[20];floatscore;};intmain(){inti,L;floats;scanf("%d",&L);st... 这是程序的主体:
struct stu
{
char name[20];
float score;
};
int main()
{
int i,L;
float s;
scanf("%d",&L);
struct stu *p;
p=(struct stu*)malloc(sizeof(struct stu)*L);
for (i=0;i<L;i++)
{
p++;
printf("input %dth score",i+1);
scanf("%f",&p[i].score);
}
……………………………………
}
我是想输出用户输入的第一个成绩。但是我输出的s永远是乱码………………求助!!省略号部分要填什么?或者程序怎么改才能把指针调回p[0]…………
展开
 我来答
寒寒家
推荐于2016-07-08 · TA获得超过2919个赞
知道大有可为答主
回答量:1482
采纳率:100%
帮助的人:1951万
展开全部

上面的代码中,没有对s进行输入,所以如果输出s的话,指定会是乱码的啊


把指针调回p[0]的话,可以p--回去


或者在p改变之前,用tmp_p保存起始位置。

struct stu *tmp_p = p;

……

p = tmp_p;


此外,我猜,这个程序中的s是为了保存平均值?

 struct stu
   {
       char name[20];
       float score;
   };
int main()
{
    int i,L;
    float s;
    scanf("%d",&L);
    struct stu *p;
    p=(struct stu*)malloc(sizeof(struct stu)*L);
    s = 0;
    for (i=0;i<L;i++)
       {
           p++;
           printf("input %dth score",i+1);
           scanf("%f",&p[i].score);
           s+=p[i].score;
        }
        s/=L; //就是平均值了
        delete p;//最后一定要释放内存
}
追问
其实,s是作为一个中间值,接下来是要比较输入成绩的大小,并输出最大值~
追答

哦 这样啊,最好起见名知意的名字max_score之类的,那么实现这个的话,只要稍微修改下就可以了:

struct stu
   {
       char name[20];
       float score;
   };
int main()
{
    int i,L;
    float s;
    scanf("%d",&L);
    struct stu *p;
    p=(struct stu*)malloc(sizeof(struct stu)*L);
    struct stu *tmp_p = p;
    s = -1; //score 都是正数
    for (i=0;i<L;i++)
       {
           p++;
           printf("input %dth score",i+1);
           scanf("%f",&p[i].score);
           if(p[i].score > s)
               s = p[i].score;
        }
        delete p;//最后一定要释放内存
}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
王勃啊
2014-12-19 · TA获得超过1.1万个赞
知道大有可为答主
回答量:5015
采纳率:62%
帮助的人:3997万
展开全部

输出第一个成绩的话这样就可以了


#include "stdio.h"
#include <string.h>
#include <stdlib.h>
   struct stu
   {
       char name[20];
       float score;
   };
int main()
{
    int i,L;
    float s;
    scanf("%d",&L);
    struct stu *p;
    p=(struct stu*)malloc(sizeof(struct stu)*L);
    for (i=0;i<L;i++)
       {
           p++;
           printf("input %dth score",i+1);
           scanf("%f",&p[i].score);
        }
    printf("%f",p[0].score);
}
追问
这样不行啊
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
kaixingui2012
2014-12-19 · TA获得超过4.2万个赞
知道大有可为答主
回答量:1.4万
采纳率:81%
帮助的人:6149万
展开全部
#include<stdio.h>
#include<stdlib.h>
struct stu
{
char name[20];
float score;
};
int main()
{
    int i,L;
    float s;

    struct stu *p;
scanf("%d",&L);
    p=(struct stu*)malloc(sizeof(struct stu)*L);
    for (i=0;i<L;i++)
{
//这里不要移动p,不然后面p就指向不正确的位置了
printf("input %dth score: ",i+1);
scanf("%f",&p[i].score);
}
printf("%f\n", p->score );
return 0;
}
追问
但是我需要用p作为指针创建链表,由于是动态空间分配,所以指针好像不能丢把?
追答
指针首位置一定不能丢!
话说,你这程序只是建立了一个数组,看不到链表的痕迹啊!
链表是用next指针串起来的一个一个的node
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式