C语言-结构体

结构体概念

由一个或多个基础类型组合出来的一个新的复合类型。

结构体类型的声明

struct student_t{
    int sn;
    char name[32];
    int age;
};

结构体变量的定义

struct student_t stu;

结构体可以在全局声明,也可以在某个函数内部进行声明,但是局部声明的类型除了作用域就无法被访问。可以在声明结构体的同时定义变量

typedef的作用

类型定义,基于一个原始的类型 ,定义出一个新的类型。

基于struct student_t类型定义了一个新的类型Student,此时的Student是一个类型。

typedef常用于复杂数据类型的类型定义,可以大大简化结构体类型名的使用。

结构体数组的定义与使用

#include<stdio.h>
#include<stdlib.h>
typedef struct student_t{
    int sn;
    char name[32];
    int age;
}Student;

typedef struct student_t Stu;

int main()
{
    struct student_t stu = { 2, "小红", 18 };
    Student stu1 = { 1, "小明", 18 };
    Stu stu2[]={
        { 1, "小明", 18 },
        { 2, "小红", 19 },
        { 3, "小蓝", 20 }
    };
    for (int i = 0; i < sizeof(stu2) / sizeof(stu2[0]); i++)
    {
        printf("学号:%3d\t姓名:%6s\t年龄:%3d\n", stu2[i].sn, (*(stu2+i)).name, (stu2+i)->age);
    }
    
    struct student_t *pstu = &stu1;
    printf("学号:%d\n", stu.sn);
    printf("姓名:%s\n", stu.name);
    printf("年龄:%d\n", stu.age);

    printf("学号:%d\n", (*pstu).sn);
    printf("姓名:%s\n", pstu->name);
    printf("年龄:%d\n", pstu->age);
    system("pause");
    return 0;
}

结构体的嵌套定义

#include<stdio.h>
#include<stdlib.h>
typedef struct student_t{
    int sn;
    char name[32];
    int age;
}Student;//Student是一个类型
typedef struct class_t{
    int id;
    char name[32];
    int stu_count;
    struct student_t stu_arry[50];//嵌套定义struct student_t类型
}Class;//Class是一个类型
int main()
{
    Class class = { 1, "一年级一班", 2, { { 1, "小明", 18 }, { 2, "小红", 19 } } };
    printf("%10s   %10s   %10s\n", "班级ID", "班级名称","班级数量");
    printf("%10d   %10s   %10d\n", class.id, class.name, class.stu_count);
    for (int i = 0; i < class.stu_count; i++)
    {
        printf("学号:%3d\t姓名:%6s\t年龄:%3d\n", class.stu_arry[i].sn, class.stu_arry[i].name, class.stu_arry[i].age);
    }
    system("pause");
    return 0;
}

结构体类型的嵌套定义与使用

结构体类型的成员自引用

结构体类型成员的类型是否可以是结构体自身类型?

stu成员定义时,student_t类型还没有定义完毕,这时候的struct student_t还不是一个完整的类型。

但是,定义结构体自身类型的指针是可以的。因为,指针类型的成员变量大小固定,跟结构体类型是否定义完毕无关。

结构体成员的存储

struct student_t{
    int sn;//4字节空间
    char name[32];//32字节空间
}stu;

stu变量的存储:结构体中每一个成员变量都要占空间,成员变量的名称就代表了一整块空间中的某个子块。

stu.sn=1;
strcpy(stu.name,"hello");//字符串的赋值

结构体变量的传参

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct studen_t{
    int sn;
    char name[32];
};
void func1(struct studen_t stu)
{
    printf("%d---%s\n", stu.sn, stu.name);
}
void func2(struct studen_t *pstu)
{
    pstu->sn = 2;
    strcpy(pstu->name, "小红");//结构体中字符串的赋值
}
int main()
{
    struct studen_t stu = { 1, "小明" };
    func1(stu);
    func2(&stu);
//传递空间地址进入函数,不仅可以访问地址所指向的空间,还可以修改所指向空间的值
    func1(stu);
    system("pause");
    return 0;
}

数组传参:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct studen_t{
    int sn;
    char name[32];
};
void func(struct studen_t *pstu)
{
    pstu->sn = 21;
    strcpy(pstu->name, "小红");//结构体中字符串的赋值
}
int main()
{
    struct studen_t arry[3] = {
        { 1, "张三" },
        { 2, "李四" },
        { 3, "王五" }
    };
    func(arry);//arry是数组名,是数组首元素的地址,传递进入参数后,即修改数组首元素的值
    for (int i = 0; i < 3; i++)
    {
        printf("学号:%2d  姓名:%s\n", arry[i].sn, arry[i].name);
    }
    system("pause");
    return 0;
}
因为结构体类型占用空间较大,所以结构体传参时一般采用传址操作(形参是地址,指针大小固定为4/8字节)。

#include"stdio.h" #include"stdlib.h" #define NULL 0 struct student { long num; char name[20]; int score[6]; struct student *next; }; void show() { printf("\nthere is a cataloge as follow.\n"); printf("***************************************\n"); printf("* *\n"); printf("* 1. create *\n"); printf("* 2. Insert *\n"); printf("* 3. print *\n"); printf("* 4. delete *\n"); printf("* 5. modify *\n"); printf("* 6. save_to_file *\n"); printf("* 7. lode from file *\n"); printf("* 8. exit *\n"); printf("***************************************\n"); printf("please input 1--8 to choice what you want:\n"); } struct student *create() { struct student *head,*p,*last; int i; int temp2; char name[20]; p=head=(struct student *)malloc(sizeof(struct student)); head->next=NULL; last=p; while(1) { last->next=p; last=p; p=(struct student *)malloc(sizeof(struct student)); /*last->next=p; last=p;*/ p->next=NULL; printf("number:"); scanf("%ld",&p->num); if(p->num==0)break; getchar(); printf("name:"); scanf("%s",p->name); printf("score:"); for(i=0;iscore[i]=temp2; } printf("next student's information.\n"); } free(p); return head; } void Insert(struct student *head) { struct student *p,*q; int score; long num; int i; printf("\nEnter the student's information you want to insert.\n"); printf("number:"); scanf("%ld",&num); q->num=num; getchar(); printf("name:"); scanf("%s",q->name); printf("score:(chinese,math,english,biology,physics,chemistry)\n"); for(i=0;iscore[i]=score; } q->next=NULL; p=head; while(p->next->numnum&&p->next!=NULL) p=p->next; q->next=p->next; p->next=q; if(p->next==NULL) p->next=q; } void delete(struct student *head) { struct student *p,*q; long num; printf("enter the student's information you want to delete.\n"); printf("number:"); scanf("%ld",&num); getchar(); p=head; while(p->next!=NULL&&p->next->num!=num) p=p->next; q=p->next; p->next=p->next->next; free(q); } void print(struct student *head) { struct student *p; int i; p=head->next; if(p==NULL) { printf("\nthere is no information.\n"); exit(0); } printf("\nnumber\tnamme\tchinese\tmath\tenglish\tbiology\tphysics\tchemistry\n"); while(p!=NULL) { printf("\n%ld\t%s",p->num,p->name); for(i=0;iscore[i]); p=p->next; } } void modify(struct student *head) { struct student *p; int choice,i; long num; char name[20]; int score[6]; printf("\nEnter what student's information you want to modify.\n"); printf("number:"); scanf("%ld",&num); getchar(); printf("\nname:"); scanf("%s",name); printf("\n"); p=head->next; while(p->num!=num&&p->name[20]!=name[20]&&p!=NULL) p=p->next; printf("\nplease choice what you want to modify:1-number 2-name 3-score.\n"); scanf("%d",&choice); switch(choice) { case 1:printf("\nEnter the true number:"); scanf("%ld",&num); p->num=num; break; case 2:printf("\nEnter the true name:"); scanf("%s",p->name); break; case 3:printf("\nEnter the right score:"); for(i=0;iscore[i]=score[i]; } break; } } void save_in(struct student *head) { struct student *p; FILE *fp; char file_name[30]; printf("please enter the file name you want to save.\n"); scanf("%s",file_name); printf("save to file:%s",file_name); if((fp=fopen(file_name,"w"))==NULL) { printf("can't open the file.\n"); exit(0); } p=head; while(p->next!=NULL) { fwrite((void*)p->next,sizeof(struct student),1,fp); p=p->next; } fclose(fp); } struct student *load_from_file() { struct student *head,*p,*last; FILE *fp; char file_name[30]; head=(struct student *)malloc(sizeof(struct student)); last=head; head->next=NULL; printf("please enter the file name you want to save.\n"); scanf("%s",file_name); printf("save to file:%s",file_name); if((fp=fopen(file_name,"r"))==NULL) { printf("can't open the file.\n"); exit(0); } p=(struct student *)malloc(sizeof(struct student)); p->next=NULL; while(fp=fread((void *)p,sizeof(struct student),1,fp)==1) { last->next=p; last=p; p=(struct student *)malloc(sizeof(struct student)); p->next=NULL; } free(p); fclose(fp); return head; } void main() { struct student *la; int choice; /*char Yes_No;*/ la=(struct student *)malloc(sizeof(struct student)); la->next=NULL; while(1) { show(); scanf("%d",&choice); switch(choice) { case 1:la=create(); break; case 2:Insert(la); break; case 3:print(la); break; case 4:delete(la); break; case 5:modify(la); break; case 6:save_in(la); break; case 7:la=load_from_file(); break; case 8:exit(0); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值