指针,引用在结构体在结构体问题!
//指针问题
#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;
}
出啥问题了????、怎么会在vc6.0中有错误????求解释。。。谢谢!!
[解决办法]
//指针问题#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); //此处取地址操作符&不需要 因为形参是Student 指针 而stu是Student数组名 //也就是Student 指针 指向数组首地址 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); // 形参是Student 的引用 数组怎么可能传的进来呢 //将 形参改成 Student数组的引用就可以了 //另外 如果不打算修改实参的值 最好在形参前加const 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; }
[解决办法]
首先,你的第一个程序是你调用的时候调用错误,你的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;
}
希望对你有帮助啊