为什么只有n=1的时候才输出其他都不行?

qq_45055368 2019-06-02 09:13:40
#include<stdio.h> #include<stdlib.h> struct student { int num; char swt[10]; int c,oop,db; int avg; }s; struct student input() { struct student swt; scanf("%d%s%d%d%d",&s.num,s.swt,&s.c,&s.oop,&s.db); s.avg=(float)(s.c+s.oop+s.db)/3; return s; } void sort(int n,struct student wt[]) { struct student temp; int i,j,index; for(i=0;i<n;i++) { index=i; for(j=i+1;j<n;j++) if(wt[j].avg>wt[index].avg) { index=j; temp=wt[i]; wt[i]=wt[index]; wt[index]=temp; } s=wt[i]; } } void output(int n,struct student *p) { int i; for(i=1;i<=n;i++,p++) { p=&s; printf("%-10d%-8s%-10d%-10d%-10d%-10d",(*p).num,(*p).swt,(*p).c,(*p).oop,(*p).db,(*p).avg); } } void main() { int n,i; struct student *p,s; struct student swt[100]; printf("请输入学生人数:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("第%d个学生:\n",i); printf("\t\t学号 姓名 C语言 OOP 数据库\n"); swt[i]=input(); p=&s; } sort(n,&swt[i]); output(n,p); system("pause"); }
...全文
86 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2019-06-03
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>

struct student
{
int num;
char swt[10];
int c,oop,db;
//int avg;
float avg;
}s;

struct student input()
{
//struct student swt;

scanf("%d%s%d%d%d",&s.num,s.swt,&s.c,&s.oop,&s.db);
s.avg = (s.c+s.oop+s.db)/3.0;

return s;
}

void sort(int n,struct student wt[])
{
struct student temp;
int i,j,index;

for(i=0;i<n;i++)
{
index=i;
for(j=i+1;j<n;j++)
if(wt[j].avg>wt[index].avg)
//{
index = j;
//index=j;
//temp=wt[i];
//wt[i]=wt[index];
//wt[index]=temp;
//}
if (index != i) {
temp=wt[i];
wt[i]=wt[index];
wt[index]=temp;
}
//s=wt[i];
}
}

void output(int n,struct student *p)
{
int i;

for(i=1;i<=n;i++,p++)
{
//p=&s;
printf("%-10d%-8s%-10d%-10d%-10d%.1f\n",(*p).num,(*p).swt,(*p).c,(*p).oop,(*p).db,(*p).avg);
}
}



//void main()
int main()
{
int n,i;
struct student *p,s;
struct student swt[100];

printf("请输入学生人数:");
scanf("%d",&n);
//for(i=1;i<=n;i++)
for(i = 0; i < n; i++)
{
printf("第%d个学生:\n",i);
printf("\t\t学号 姓名 C语言 OOP 数据库\n");
swt[i] = input();
//p=&s;
}

//sort(n,&swt[i]);
sort(n, swt);
p = swt;
output(n,p);
system("pause");
}


你参考~
棉猴 2019-06-03
  • 打赏
  • 举报
回复
我试着改了以下,不知道是不是你期望的结果。
void output(int n, struct student *p)

{

int i;

for (i = 1; i <= n; i++, p++)

{

//p = &s;//-----------注释掉该行-------------

printf("%-10d%-8s%-10d%-10d%-10d%-10d", (*p).num, (*p).swt, (*p).c, (*p).oop, (*p).db, (*p).avg);

}

}

在main()函数中
	for (i = 1; i <= n; i++)

{

printf("第%d个学生:\n", i);

printf("\t\t学号 姓名 C语言 OOP 数据库\n");

swt[i-1] = input();//----------修改--------------

p = swt;//----------------修改----------------

}

以下是VS2015调试的结果
qq_45055368 2019-06-03
  • 打赏
  • 举报
回复
引用 1 楼 qq_43218782的回复:
在main函数里的输入的for循环里p指针每次都重新指向了另一个地址了
应该怎么改呢那么应该怎么改呢
qq_43218782 2019-06-03
  • 打赏
  • 举报
回复
在main函数里的输入的for循环里p指针每次都重新指向了另一个地址了

70,036

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧