| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 517 人关注过本帖
标题:麻烦看下这个程序
只看楼主 加入收藏
aiyinsitan
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:183
专家分:177
注 册:2010-4-22
结帖率:89.47%
收藏
已结贴  问题点数:20 回复次数:3 
麻烦看下这个程序
#include <iostream.h>
#include <string.h>
#include <malloc.h>
#define LEN sizeof(struct student)//student 这个结构的长度
struct student  //定义结构
{
    long num;
    char name[20];
    float score[30];
};
void input(struct student *p);//输入函数
void print(struct student *p);//输出函数
void main()
{
    int i;
    struct student *p[5];//定义一个指针数组
    for(i=0;i<1;i++)
    {
      input(p[i]);
    }
    for(i=0;i<1;i++)
    {
    print(p[i]);
    }
}
void input(struct student *p)
{
    int j;
  p=(struct student *)malloc(LEN);
         cin>>p->num;
     cin>>p->name;
     for(j=0;j<3;j++)
     {
         cin>>p->score[j];
     }
}
void print(struct student *p)
{
     int i;
     //struct student *p1;
     //p=(struct student *)malloc(LEN);
     //p1=p;
     cout<<p->num<<"  ";
     cout<<p->name<<"  ";
     for(i=0;i<3;i++)
     {
         cout<<p->score[i];
     }
     cout<<endl;
}
总共有5个学生,三门成绩,用input函数输入,用print函数输出 输入没问题  但输出的时候  提示内存提示错误
搜索更多相关主题的帖子: void 函数 include 
2011-04-02 13:21
aiyinsitan
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:183
专家分:177
注 册:2010-4-22
收藏
得分:0 
#include <iostream.h>
#include <string.h>
#include <malloc.h>
#define LEN sizeof(struct student)//student 这个结构的长度
struct student  //定义结构
{
    long num;
    char name[20];
    float score[30];
};
void input(struct student *p);//输入函数
void print(struct student *p);//输出函数
void main()
{
    int i;
    struct student *p[5];//定义一个指针数组
    for(i=0;i<5;i++)
    {
     p[i]=(struct student *)malloc(LEN);
      input(p[i]);
    //print(p[i]);
    }
    for(i=0;i<5;i++)
    {
    print(p[i]);
    }
}
void input(struct student *p)
{
    int j;
  
         cin>>p->num;
     cin>>p->name;
     for(j=0;j<3;j++)
     {
         cin>>p->score[j];
     }
}
void print(struct student *p)
{
     int i;
     //struct student *p1;
     //p=(struct student *)malloc(LEN);
     //p1=p;
     cout<<p->num<<"  ";
     cout<<p->name<<"  ";
     for(i=0;i<3;i++)
     {
         cout<<p->score[i];
     }
     cout<<endl;
}
2011-04-02 13:57
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:20 
第二个输出正常,你想说什么呢?忘了分配内存就开始接收数据?

我们都在路上。。。。。
2011-04-02 14:19
aiyinsitan
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:183
专家分:177
注 册:2010-4-22
收藏
得分:0 
回复 3楼 hellovfp
恩了  SB 了
2011-04-03 16:30
快速回复:麻烦看下这个程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011299 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved