请教结构体问题

chenzuzhang 2012-02-27 07:38:29
编译,连接都没问题,但是运行的是后出错,不解。尝试着把与求平均值(ave)的相关语句删除掉,结果就没问题,请教各位。
#include<stdio.h>
#define N 3 //学生人数
struct information
{
char name[20];
char ID[15];
int chi;
int mat;
int eng;
int phy;
int spo;
int sum;
int ave;}; //学生信息
struct information student,*p1;
get(struct information *pa) //录入学生信息函数
{
int i;
for(i=0;i<N;i++)
{
scanf("%s%s%d%d%d%d%d",(*pa).name,(*pa).ID,&(*pa).chi,&(*pa).mat,&(*pa).eng,&(*pa).phy,&(*pa).spo);
(*pa).sum=(*pa).chi+(*pa).mat+(*pa).eng+(*pa).phy+(*pa).spo;
(*pa).ave=(*pa).sum/5;
pa++;
}
}
out(struct information *pb) //输出学生信息函数
{
int i;
for(i=0;i<N;i++)
{
printf("%s %s %d %d %d %d %d %d %d \n",(*pb).name,(*pb).ID,(*pb).chi,(*pb).mat,(*pb).eng,(*pb).phy,(*pb).spo,(*pb).sum,(*pb).ave);
pb++;
}
}
main()
{

printf("请输入%d个学生的信息,每输入完一个按回车键:\n",N);
p1=&student;
get(p1);
printf("姓名 学号 语文 数学 英语 物理 体育 总分 均分\n");
out(p1);
}
...全文
151 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhsgbc 2012-03-07
  • 打赏
  • 举报
回复
去掉int spo应该还是不行,spo储存在栈中,只要你输入的数据多于一个,栈中的其他数据就会被覆盖,可能还会覆盖函数的返回地址,后果很严重,呵呵。修改方法也可以用数组也可以用链表,感觉链表要好些,毕竟数组被储存在栈中,到时栈溢出就不妙了。[Quote=引用 5 楼 chenzuzhang 的回复:]
引用 1 楼 w170532934 的回复:
你只有一个内存空间,为什么还要进行指针++呢??你的指针指向了未知的内存,行为不确定了。如果你需要链表,你需要设计你的结构体了。

我想再请教一下,那为什么我去掉了int spo;有关项就没有问题了呢?难道此时就能使用p++了吗?谢谢,我是菜鸟。
[/Quote]
kason2011 2012-03-07
  • 打赏
  • 举报
回复
#define N 3 //学生人数
struct information
{
char name[20];
char ID[15];
int chi;
int mat;
int eng;
int phy;
int spo;
int sum;
int ave;
}; //学生信息

struct information student[N],*p1;

void get(struct information *pa) //录入学生信息函数
{
int i;
for(i=0;i<N;i++)
{
scanf("%s%s%d%d%d%d%d",&(*(pa+i)).name,&(*(pa+i)).ID,&(*(pa+i)).chi,&(*(pa+i)).mat,&(*(pa+i)).eng,&(*(pa+i)).phy,&(*(pa+i)).spo);
(*(pa+i)).sum=(*(pa+i)).chi+(*(pa+i)).mat+(*(pa+i)).eng+(*(pa+i)).phy+(*(pa+i)).spo;
(*(pa+i)).ave=(*(pa+i)).sum/5;
pa++;
}
}

void out(struct information *pb) //输出学生信息函数
{
int i;
for(i=0;i<N;i++)
{
printf( "%s %s %d %d %d %d %d %d %d \n",(*(pb+i)).name,(*(pb+i)).ID,(*(pb+i)).chi,(*(pb+i)).mat,(*(pb+i)).eng,(*(pb+i)).phy,(*(pb+i)).spo,(*(pb+i)).sum,(*(pb+i)).ave );
pb++;
}
}

int main()
{

printf("请输入%d个学生的信息,每输入完一个按回车键:\n",N);
p1=&student[0];
get(p1);
printf("姓名 学号 语文 数学 英语 物理 体育 总分 均分\n");
out(p1);
return 0;
}
kason2011 2012-03-07
  • 打赏
  • 举报
回复
用数组就可以了
先试试贴代码的格式,上次不成功
#include<iostream>
ls176 2012-02-29
  • 打赏
  • 举报
回复
struct information *next 知识点就是链表
面包大师 2012-02-27
  • 打赏
  • 举报
回复
struct information *next;//定义了一个struct information 类型的指针next,类似int *p;这样去理解
chenzuzhang 2012-02-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ls176 的回复:]
C/C++ code


#include<stdio.h>
#define N 3 //学生人数
struct information
{
char name[20];
char ID[15];
int chi;
int mat;
int eng;
int phy;
int spo;
int sum;
……
[/Quote]
谢谢,还想请教一个问题,struct information *next;是什么意思?它的用途我明白了,只是我不知道这个知识点叫什么,谢谢!
chenzuzhang 2012-02-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 w170532934 的回复:]
你只有一个内存空间,为什么还要进行指针++呢??你的指针指向了未知的内存,行为不确定了。如果你需要链表,你需要设计你的结构体了。
[/Quote]
我想再请教一下,那为什么我去掉了int spo;有关项就没有问题了呢?难道此时就能使用p++了吗?谢谢,我是菜鸟。
ls176 2012-02-27
  • 打赏
  • 举报
回复

#include<stdio.h>
#define N 3 //学生人数
struct information
{
char name[20];
char ID[15];
int chi;
int mat;
int eng;
int phy;
int spo;
int sum;
int ave;
struct information *next;
}; //学生信息
struct information student , *p1;
void get( struct information *pa ) //录入学生信息函数
{
int i;
struct information *tmp;
tmp = pa;
for( i = 0 ; i < N ; i++ )
{
scanf( "%s%s%d%d%d%d%d" , ( *tmp ).name , ( *tmp ).ID , &( *tmp ).chi , &( *tmp ).mat , &( *tmp ).eng , &( *tmp ).phy , &( *tmp ).spo );
( *tmp ).sum=( *tmp ).chi + ( *tmp ).mat+( *tmp ).eng + ( *tmp ).phy + ( *tmp ).spo;
( *tmp ).ave=( *tmp ).sum / 5;
if( i < N - 1 )
{
tmp->next = ( struct information * )malloc( sizeof( struct information ) );
tmp = tmp->next;
}
}
}

void out( struct information *pb ) //输出学生信息函数
{
int i;
for( i = 0 ; i < N ; i++ )
{
printf( "%s %s %d %d %d %d %d %d %d \n" , ( *pb ).name , ( *pb ).ID , ( *pb ).chi , ( *pb ).mat , ( *pb ).eng , ( *pb ).phy , ( *pb ).spo , ( *pb ).sum , ( *pb ).ave );
pb = pb->next;
}
}

main()
{
printf( "请输入%d个学生的信息,每输入完一个按回车键:\n" , N );
p1 = &student;
get( p1 );
printf("姓名 学号 语文 数学 英语 物理 体育 总分 均分\n");
out( p1 );
}

做了点小修改
quwei197874 2012-02-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 w170532934 的回复:]

你只有一个内存空间,为什么还要进行指针++呢??你的指针指向了未知的内存,行为不确定了。如果你需要链表,你需要设计你的结构体了。
[/Quote]++
y8758622 2012-02-27
  • 打赏
  • 举报
回复
需要设计结构体吧,兄台~
W170532934 2012-02-27
  • 打赏
  • 举报
回复
你只有一个内存空间,为什么还要进行指针++呢??你的指针指向了未知的内存,行为不确定了。如果你需要链表,你需要设计你的结构体了。

69,371

社区成员

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

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