C构造类型学习
一、C语言结构体
1、结构体的入门:
代码示例如下:
/*C语言结构体*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
//结构体说明
struct Time{
char name[100];
int age;
int number;
};
//结构体变量和说明于一体
struct Time2{
char name[100];
int age;
int number;
}me;
//结构体中包含指针
struct Info{
char *name;
int number;
};
int main(){
struct Time you;//结构体变量声明
strcpy(you.name,"huangffh");//结构体赋值
you.age=19;//结构体变量的存取引用
you.number=90;
//结构体访问
printf("%s %d %d\n",you.name,you.age,you.number);
strcpy(me.name,"ffh");//结构体赋值
me.age=23;
me.number=1;
printf("%s %d %d\n",me.name,me.age,me.number);
//结构体数组
struct Time he[3];
for(int i=0;i<3;i++){//赋值
strcpy(he[i].name,"ffh%d");
he[i].age=i+23;
he[i].number=12+i;
}
for(int i=0;i<3;i++) printf("%s %d %d\n",he[i].name,he[i].age,he[i].number);//取值
//结构体指针
struct Time *p;
p=&you;
printf("%s %d %d\n",p->name,p->age,(*p).number);//指针访问结构体成员的两种方法
static struct Info me1={"u",30};
printf("%s %d\n",me1.name,me1.number);//取值
return 0;
}
2、结构体的提高:
结构说明不能递归,也就是结构体说明不能包含自身的结构体作为成员。
struct Time{
char *name;
struct Time time;//错误
struct Time *time;//正确 在C语言中允许将自身的结构体指针作为结构体成员
};
1、代码
/*C语言结构体*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
//结构体说明
struct Time{
char name[100];
int age;
int number;
struct Time *next;//指向自己一样的结构体指针
}*q,*p;
int main(){
static struct Time you={"ffh",19,1};//结构体变量声明
static struct Time you1={"sbx",18,2};
q=&you1;
p=&you;
p->next=q;
q->next=NULL;
printf("you.name=%s you.age=%d you.number=%d\n",you.name,you.age,you.number);
printf("you1.name=%s you1.age=%d you1.number=%d\n",you1.name,you1.age,you1.number);
printf("p->name=%s p->age=%d p->number=%d\n",p->name,p->age,p->number);
printf("(p->next)->name=%s (p->next)->age=%d (p->next)->number=%d\n",(p->next)->name,(p->next)->age,(p->next)->number);
printf("q->name=%s q->age=%d q->number=%d\n",q->name,q->age,q->number);
return 0;
}
结果如下:
2、代码
//*链表走链*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
struct List{
char name[200];
int age;
struct List *next;
}*p,*head;
int main(){
//为结构体变量申请内存,p和head同时指向一个节点
head=p=(struct List *)malloc(sizeof(struct List));
if(p==NULL)exit(0);
puts("please enter name and age:");
scanf("%s %d",p->name,&(p->age));
while(p->age!=300){//循环输入不同的人的信息
//申请下一个人的存贮空间
p->next=(struct List*)malloc(sizeof(struct List));
if(p->next==NULL)exit(0);
p=p->next;//移动到下一个存贮节点
puts("please enter name and age:");
scanf("%s %d",p->name,&(p->age));
}
p->next=NULL;
p=head;//p指向开始节点
while(p!=NULL){
printf("%s %d\n",p->name,p->age);
p=p->next;
}
return 0;
}
3、结构体和函数
(1)、结构体数据作为函数参数传递
#include<cstdio>
#include<cstdlib>
#include<cstring>
struct PLAY{
char name[100];
int age;
};
void show1(char *name,int age){
printf("%s %d\n",name,age);
}
void show2(struct PLAY *p,int size){
struct PLAY *q;
for(q=p;q<p+size;q++){
printf("%s %d\n",q->name,q->age);
}
}
int main(){
struct PLAY p[2]={
{"ffh",2314},
{"sbx",2324}
};
show2(p,2);
show1(p[1].name,p[0].age);
return 0;
}
(2)、返回指向结构体的指针
#include<cstdio>
#include<cstdlib>
#include<cstring>
struct List{
char name[200];
int age;
struct List *next;
}*p,*head;
struct List *play(){
struct List *p;
//为结构体变量申请内存,p和head同时指向一个节点
head=p=(struct List *)malloc(sizeof(struct List));
if(p==NULL)exit(0);
puts("please enter name and age:");
scanf("%s %d",p->name,&(p->age));
while(p->age!=300){//循环输入不同的人的信息
//申请下一个人的存贮空间
p->next=(struct List*)malloc(sizeof(struct List));
if(p->next==NULL)exit(0);
p=p->next;//移动到下一个存贮节点
puts("please enter name and age:");
scanf("%s %d",p->name,&(p->age));
}
p->next=NULL;
return head;
}
void show(struct List *head){
struct List *p;
p=head;
while(p!=NULL){
printf("%s %d\n",p->name,p->age);
p=p->next;
}
}
int main(){
struct List *head;
head=play();
show(head);
return 0;
}
(3)、结构体的嵌套以及写入文件
#include<cstdio>
#include<cstdlib>
#include<cstring>
struct P_ONE{
char name[200];
char age;
};
struct P_TWO{
struct P_ONE play;
int number;
int money;
};
int main(){
struct P_TWO play1;
strcpy(play1.play.name,"fhf");//嵌套初始化
play1.number=100;
printf("%s %d\n",play1.play.name,play1.number);//嵌套访问
//将结构体数据写入文件
FILE *fp;
if((fp=fopen("d://1234.txt","w"))==NULL) /* c盘下创建c:\123.txt文件*/
{
printf("\nopen file error");
getchar();
exit(1);
}
fprintf(fp,"%s",play1.play.name);
fprintf(fp,"%d",play1.number);
fclose(fp);
return 0;
}
(4)、C99的伸缩型数组成员
使用规则:
1、伸缩型数组成员必须是结构体的最后一个成员
2、结构体至少有一个其他的成员
3、伸缩型成员向不同函数一样声明,除了它的[]里是空的。
代码:
#include<cstdio>
#include<cstdlib>
struct PLAY{
double score;
char name[100];
double ave[];//c99中的可伸缩型数组
};
int main(){
struct PLAY *p1;
int n=2;
double tot=0;
p1=(struct PLAY*)malloc(sizeof(struct PLAY)+n*sizeof(double));
p1->score=n;
for(int i=0;i<n;i++){
p1->ave[i]=99-i*2;
tot+=p1->ave[i];
}
for(int i=0;i<n;i++){
printf("%f ",p1->ave[i]);
}
printf(" %f",tot);
return 0;
}
二、联合体、枚举
1、联合体(公用体):联合体声明时不引起内存的分配,联合是一个能在同一个存贮空间里存放不同类型的数据类型。
格式:
union 联合名{
成员表;
};
联合变量定义的三种方法:(例子)
(1)、union PLAY{
char name[100];
int age;
};
union PLAY p;
(2)、union PLAY{
char name[100];
int age;
}p;
(3)、union{
char name[100];
int age;
}p;
注意:联合体变量在内存中占字节数是成员表中最大的,而不像结构体中是成员表的总和。
联合程序示例:
#include<cstdio>
#include<cstdlib>
#include<cstring>
struct Student{
char stu_num[10];
char course[12];
int kind;
union{
char c;
int s;
}score;
};
void accept(struct Student *st,int size){
int i;
for(i=0;i<size;i++){
scanf("%s%s%d",st[i].stu_num,st[i].course,&st[i].kind);
if(st[i].kind==1){
scanf("%c",&st[i].score);
}else{
scanf("%d",st[i].score);
}
}
}
int find(struct Student *st,int size){
int i,flag=0;
char temp[8];
puts("Please input the student number:");
scanf("%s",temp);
for(i=0;i<size;i++){
if(strcmp(temp,st[i].stu_num)==0){
if(st[i].kind==1){
printf("%s %s %S %c\n",st[i].stu_num,st[i].course,"elective",st[i].score);
}else{
printf("%s %s %S %c\n",st[i].stu_num,st[i].course,"need",st[i].score);
}
flag=1;
}
}
return flag;
}
int main(){
int i;
struct Student st[6];
accept(st,6);
i=find(st,6);
if(i==0)puts("\nnot found!");
return 0;
}
2、枚举的使用是提高程序的可读性
格式:
enum 枚举名{
枚举元素表;
};
枚举变量定义的三种方法:(例子)
(1)、enum PLAY{
char name[100];
int age;
};
enum PLAY p;
(2)、enum PLAY{
char name[100];
int age;
}p;
(3)、enum {
char name[100];
int age;
}p;
enum 中的数据默认是比前一个加1的:
typedef enum
{
aa= 1,
bb, //bb= 2
cc= 2,
dd, //dd = 3
}hu;
三、typedef简介
typedef是一种高级数据结构,它能为某以数据类型创建自己的名字。
它与#define的三种不同之处:
1、typedef给出的符号名仅限于对类型而不是对值;
2、typedef解释由编译器执行而不是预处理器;
3、虽然它范围有限,但是在受限的范围内比#define更灵活。
四、C++的结构体
在C语言中,结构中不允许有成员函数,但是在c++中可以有成员函数。
c++类与结构的唯一区别就是:类定义默认境况下是private,而在结构定义的默认境况下是public。
(全文完)