10结构体
结构类型定义的典型格式:
struct <结构名> { <成员列表> };
例子:
struct student
{ int num;
char name[20];
char sex;
float score;
};
结构变量声明的典型格式:
<结构名> <变量列表> ;
例子:
student stud1, stud2,
*sptr, stu[20];
同时进行结构类型定义和结构变量声明
例子:
struct student
{ int num;
char name[20];
char sex;
float score;
} stud1, stud2, *sptr;
结构类型student可用于声明其它变量
用无名结构类型进行结构变量声明
例子:
struct
{ int num;
char name[20];
char sex;
float score;
} stud1, stud2, *sptr;
无名结构类型无法用于声明其它变量
结构的定义可以嵌套
例子:
struct date {int year; int month;
int day; };
struct people {int num; char sex;
char name[20];
date birthday;
};
people wang;
注意:
结构定义时不能定义本类型的成员
例子:
struct fdate
{int year; int month; int day;
fdate *fptr;
fdate birthday;
};
结构变量的初始化
例子:
student stud5 =
{ 102, "Li Xiaoming", 'M', 92 };
student class[2] = {
{ 102, "Li Xiaoming", 'M', 92 },
{ 105, “Wang Ming", ‘F', 88 } };
结构变量的使用
1.同类型结构变量可直接赋值,如stud1 = stud2;
2.用圆点运算符 . 和箭头运算符 -> 对结构的成员进行存取,如
printf( "%s", stud1.name );
wang.birthday.year = 1986;
sptr = &stud1;
printf( "%s", sptr->name );
(sptr->name 等价于 (*sptr ).name )
结构与函数
结构成员可以作为函数的实在参数
假设有如下函数原型定义:
void f1(int);
void f2(float);
void f3(char *);
void f4(int *);
void f5(float *);
声明结构变量:
struct student stud;
下面的函数调用是合法的:
f1(stud.num); //传递stud.num
f1(stud.name[2]);
f2(stud.score); //传递stud.score的值
f3(stud.name);
f3(&stud.name[2]);
f4(&stud.num);//传递成员stud.num的地址
f5(&stud.score);
函数的参数或返回值可以是结构类型
函数原型:
void f6(student s);
student f7(student & s);
student *f8(student * s);
函数调用:
f6(stud);
printf( "%s", f7(stud).name );
printf( "%s", f8(&stud)->name );
结构与数组
结构成员可以是数组;数组元素可以是结构(结构数组)。如
struct student
{ int num;
char name[20];
char sex;
float score;
}class[5];
结构与指针
结构成员可以是指针;指针可以指向结构(结构指针),也可以作为结构成员指向本结构。如
struct fdate
{int year; int month; int day;
fdate *fptr;
student *Sptr;
};
student stud, *pStud=&stud;
(*pStud).num = 101;
pStud->score = 95;
枚举
是一种类型,值为标识符(有限集),每个标识符的内部表示是一个整数。枚举类型的定义如
enum weekday {Sun,Mon,Tue,Wed,Thu,Fri,Sat};
枚举常量:枚举类型定义中的标识符,与一般符号常量类似,但可自动定值,起始值从 0 开始,递增值为 1
可以用 = 为枚举常量定值
枚举类型定义中的标识符必须唯一
枚举变量声明:与一般变量相同
枚举变量只能被赋予相应的枚举常量
示例:
enum weekday1 {Sun=1,Mon,Tue,Wed,Thu,Fri,Sat};
enum weekday2 {Sun=1,Mon,Tue=5,Wed,Thu,Fri=10,Sat};
weekday1 day1 ; weekday2 day2 ;
day1 = Mon;//等价于 day1 = (weekday1)2
day2 = Wed;//等价于 day2 = (weekday2)6
cout<< day1 <<endl<< day2;
例子一:结构体作为函数的参数
#include <iostream.h>
struct student
{
int num;
char name [ 20 ];
char sex;
float score;
};
void displayStudentInfo(
const student & stud)
{cout << endl;
cout << "num = " << stud.num << "\t";
cout << "name = " << stud.name << "\t";
cout << "sex = " << stud.sex << "\t";
cout << "score = "<<stud.score<<endl;
}
void funCallByValue (student stud)
{stud.score ++; }
void funCallByReference (student &stud)
{stud.score ++; }
void funCallByPointer (student * stud)
{stud->score ++; }
main()
{student theStud =
{ 102, "Li Xiaoming", 'M', 92 };
cout << "Initial information:";
displayStudentInfo ( theStud ) ;
funCallByValue ( theStud ) ;
cout << "\nAfter call by value:";
displayStudentInfo ( theStud ) ;
funCallByReference ( theStud ) ;
cout << "\nAfter call by reference:";
displayStudentInfo ( theStud ) ;
funCallByPointer ( & theStud );
cout << "\nAfter call by pointer:";
displayStudentInfo ( theStud );
return 0;
}
例子二:函数以结构体为返回值
#include <iostream.h>
struct student
{int num;
char name[20];
char sex;
float score;
};
void displayStudentInfo(
const student & stud)
{cout << endl;
cout << "num = " << stud.num << "\t";
cout << "name = " << stud.name << "\t";
cout << "sex = " << stud.sex << "\t";
cout << "score = "<< stud.score<< endl;
}
student getStudent()
{
student stud;
cout << "Please enter the number: ";
cin >> stud.num;
cout << "Please enter the name: ";
cin >> stud.name;
cout << "Please enter the sex: ";
cin >> stud.sex;
cout << "Please enter the score: ";
cin >> stud.score;
return stud;
}
main()
{
student theStud = { 102, "Li Xiaoming", 'M', 92 };
cout << "Initial student information:";
displayStudentInfo(theStud);
theStud = getStudent();
cout << "\nAfter call getStudent:";
displayStudentInfo(theStud);
return 0;
}
例子三:将学生记录按学号大小排序
#include <iostream.h>
#define STUDENT_Num 5
struct student
{ int num;
char name[20];
char sex;
float score;
};
void displayStudentsInfo(
const student studs[], int len)
{for (int i = 0; i < len ; i ++ )
{cout << "num = "
<< studs[ i ].num << "\t";
cout << "name = "
<< studs[ i ].name << "\t";
cout << "sex = "
<< studs[ i ].sex << "\t";
cout << "score = "
<< studs[ i ].score << endl;
}
}
void sortArray(student studs[],int len)
{for (int pass = 0;
pass < len - 1; pass ++ )
for ( int i = pass + 1;
i <= len - 1; i ++ )
if(studs[pass].num>studs[i].num )
{ student hold;
hold = studs[ pass ];
studs[ pass ] = studs[ i ];
studs[ i ] = hold;
}
}
main( )
{student theClass [STUDENT_Num] =
{ {110, "Zhang Ping", 'M', 45},
{102, "Li Xiaoming", 'M', 92},
{153, "Wang Ming", 'M', 52.5},
{134, "Cheng Ling", 'F', 87},
{105, "Wang Xiaofang", 'F', 95}};
cout <<"Initial student information:\n";
displayStudentsInfo(theClass,
STUDENT_Num );
cout << "\nAfter sorting:\n";
sortArray ( theClass, STUDENT_Num );
displayStudentsInfo(theClass,
STUDENT_Num );
return 0;
}
例子四:结构指针变量的声明和使用
#include <iostream.h>
struct student
{int num;
char name[20];
char sex;
float score; };
main()
{student stud =
{102, "Li Xiaoming", 'M', 92};
student * pStud = & stud;
cout<< "num = " << stud.num << "\t";
cout << "name = " << stud.name << "\t";
cout << "sex = " << stud.sex << "\t";
cout << "score = "<< stud.score<< endl;
cout << "Access structure "
" through pointer and ( . ):\n";
cout << "num = " << (*pStud).num << "\t";
cout <<"name = "<<(*pStud).name << "\t";
cout <<"sex = "<<(*pStud).sex << "\t";
cout <<"score = "<<(*pStud).score<<endl;
cout<<"Access structure "
" through pointer and ( -> ):\n";
cout << "num = " << pStud->num << "\t";
cout << "name = " << pStud->name << "\t";
cout << "sex = " << pStud->sex << "\t";
cout <<"score = "<< pStud->score<< endl;
return 0;
}