QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2227|回复: 2
打印 上一主题 下一主题

关于指针在结构体的问题

[复制链接]

3

主题

4

听众

86

积分

群组C 语言讨论组

群组数学建模

群组Matlab讨论组

群组计算机考研

跳转到指定楼层
1#
发表于 2012-2-20 17:42 |只看该作者 |倒序浏览
//指针问题
#include<iostream>
#include<iomanip>
using namespace std;

struct Student
{
        int num;
        char name[20];
        float score[3];
};

int main()
{
        Student stu[5];
        void print(Student *);
        cout<<"input information of five students:"<<endl;
        for(int i=0;i<5;i++)
        {cout<<"第"<<i+1<<"个学生信息:  "<<endl;
        cin>>stu[i].num>>stu[i].name>>stu[i].score[0]
                >>stu[i].score[1]>>stu[i].score[2];
        }

        print(&stu);

        return 0;
}

void print(Student *p)
{
        cout<<endl;
        cout<<"num"<<setw(4)<<"name"<<setw(4)<<"score1"
                <<setw(4)<<"score2"<<setw(4)<<"score3"<<endl;
        for(int j=0;j<5;j++)
        cout<<p->num<<setw(3)<<p->name<<setw(3)<<p->score[0]
        <<setw(3)<<p->score[1]<<setw(3)<<p->score[2]<<endl;
   
}      
//引用问题
#include<iostream>
#include<iomanip>
using namespace std;

struct Student
{
        int num;
        char name[20];
        float score[3];
};

int main()
{
        Student stu[5];
        void print(Student &);
        cout<<"input information of five students:"<<endl;
        for(int i=0;i<5;i++)
        {cout<<"第"<<i+1<<"个学生信息:  "<<endl;
        cin>>stu[i].num>>stu[i].name>>stu[i].score[0]
                >>stu[i].score[1]>>stu[i].score[2];
        }

        print(stu);

        return 0;
}

void print(Student &stud)
{
        cout<<endl;
        cout<<"num"<<setw(4)<<"name"<<setw(4)<<"score1"
                <<setw(4)<<"score2"<<setw(4)<<"score3"<<endl;
        for(int j=0;j<5;j++)
        cout<<stud[j].num<<setw(3)<<stud[j].name<<setw(3)<<stud[j].score[0]
        <<setw(3)<<stud[j].score[1]<<setw(3)<<stud[j].score[2]<<endl;
   
}      



出啥问题了????、怎么有错误????求解释。。。谢谢!!
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
海阔凭鱼跃,天高任鸟飞!
806133023        

0

主题

0

听众

23

积分

群组C 语言讨论组

2#
发表于 2012-2-20 21:06 |只看该作者
首先,你的第一个程序是你调用的时候调用错误,你的main函数定义的是一个数组,调用的时候stu已经是地址了,所以就不用那个地址的符号;第二个引用符号,引用的不是地址,所以在调用引用形参变量的函数的时候,直接写变量的名字就可以了,不是地址,具体的程序如下:
第一个:
#include<iostream>
#include<iomanip>
using namespace std;

struct Student
{
        int num;
        char name[20];
        float score[3];
};

int main()
{
        Student stu[5];
        void print(Student *);
        cout<<"input information of five students:"<<endl;
        for(int i=0;i<5;i++)
        {cout<<"第"<<i+1<<"个学生信息:  "<<endl;
        cin>>stu.num>>stu.name>>stu.score[0]
                >>stu.score[1]>>stu.score[2];
        }

       print(stu);

        return 0;
}

void print(Student *p)
{
        cout<<endl;
        cout<<"num"<<setw(4)<<"name"<<setw(4)<<"score1"
                <<setw(4)<<"score2"<<setw(4)<<"score3"<<endl;
        for(int j=0;j<5;j++)
        cout<<p->num<<setw(3)<<p->name<<setw(3)<<p->score[0]
        <<setw(3)<<p->score[1]<<setw(3)<<p->score[2]<<endl;
   
}      
第二个:
#include<iostream>
#include<iomanip>
using namespace std;

struct Student
{
        int num;
        char name[20];
        float score[3];
};

int main()
{
        Student stu[5];
        void print(Student &);
        cout<<"input information of five students:"<<endl;
        for(int i=0;i<5;i++)
        {cout<<"第"<<i+1<<"个学生信息:  "<<endl;
        cin>>stu.num>>stu.name>>stu.score[0]
                >>stu.score[1]>>stu.score[2];
        }

        print(*stu);

        return 0;
}

void print(Student &stud)
{
        cout<<endl;
        cout<<"num"<<setw(4)<<"name"<<setw(4)<<"score1"
                <<setw(4)<<"score2"<<setw(4)<<"score3"<<endl;
        for(int j=0;j<5;j++)
        cout<<stud.num<<setw(3)<<stud.name<<setw(3)<<stud.score[0]
        <<setw(3)<<stud.score[1]<<setw(3)<<stud.score[2]<<endl;

   
}      
回复

使用道具 举报

工程兵        

0

主题

4

听众

26

积分

自我介绍
一个在校的热爱数学的学生

群组C 语言讨论组

群组Matlab讨论组

群组数学建模培训课堂1

3#
发表于 2012-7-15 21:27 |只看该作者
回复

使用道具 举报

qq
收缩
  • 电话咨询

  • 04714969085

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2024-4-27 10:18 , Processed in 0.391153 second(s), 57 queries .

回顶部