C++指针在使用之前一定要初始化,否则指针指向一个不可预知的内存区域,导致程序出错甚至系统崩溃。
InBlock.gif#include <iostream>  
InBlock.gif using namespace std;  
InBlock.gif struct student  
InBlock.gif{  
InBlock.gif     int idNumber;  
InBlock.gif     char* name;  
InBlock.gif     int age;  
InBlock.gif     char* department;  
InBlock.gif     float gpa;  
InBlock.gif};  
InBlock.gif void stuOut(student stu);  
InBlock.gif int main()  
InBlock.gif{  
InBlock.gif    student s1,s2;  
InBlock.gif    cout<< "请输入学号:";  
InBlock.gif    cin>>s1.idNumber;  
InBlock.gif    cout<<endl<< "请输入姓名:";  
InBlock.gif    s1.name= new char[]; //为指针申请内存;
InBlock.gif    cin>>s1.name;  
InBlock.gif    cout<<endl<< "请输入院系:";  
InBlock.gif    s1.department= new char[]; //为指针申请内存  
InBlock.gif    cin>>s1.department;  
InBlock.gif    cout<<endl<< "请输入年龄:";  
InBlock.gif    cin>>s1.age;  
InBlock.gif    cout<<endl<< "请输入成绩:";  
InBlock.gif    cin>>s1.gpa;  
InBlock.gif    s2=s1;  
InBlock.gif    stuOut(s2);  
InBlock.gif     return 0;  
InBlock.gif}  
InBlock.gif void stuOut(student stu)  
InBlock.gif{  
InBlock.gif    cout<< "s2的学号:"<<stu.idNumber<<endl;  
InBlock.gif    cout<< "S2的姓名:"<<stu.name<<endl;  
InBlock.gif    cout<< "s2的院系:"<<stu.department<<endl;  
InBlock.gif    cout<< "s2的年龄:"<<stu.age<<endl;  
InBlock.gif    cout<< "s2的成绩:"<<stu.gpa<<endl;  
InBlock.gif}