struct 后面的 student 是结构标记

结构体概述 : 结构体是 多个 变量的集合, 变量的类型可以不同;

-- 可进行的操作 : 结构体可以进行 拷贝 赋值操作, 可以作为 函数参数 和 函数返回值;


1. 结构体的基本使用


结构体声明 : struct 结构标记 {结构成员} 普通变量;

-- 结构体示例 : 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct student  
  2. {  
  3.     char *name;  
  4.     int age;  
  5. };  

--  结构标记  : struct 后面的 student 是结构标记, 这个标记 可写 可不写, 其作用是 为结构命名,  结构标记可以代表 {} 中的声明的所有的成员变量 ;

-- 结构成员 : 在 {} 中定义的变量就是结构成员;

-- 普通变量 : 在声明结构体的时候后面可以加上若干普通变量, 相当于定义结构体变量;



结构体变量声明 : 可以在定义的时候声明变量, 也可以在定义完结构体使用 结构标记 声明变量;

-- 定义结构体时声明变量 : 这种声明变量的方式可以不用 结构标记, 变量名写在 花括号 后面, 用头号隔开;

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct student  
  2. {  
  3.     char *name;  
  4.     int age;  
  5. } s1, s2, s3;  

--  使用结构标记声明  : 结构标记 student 代表了花括号的声明, 是 结构体的简写, 可以使用结构标记代替花括号中的内容;

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct student s4, s5, s6;  




结构体内存分配 : 结构体内存是在声明变量的时候分配的, 如果只声明了结构体, 没有声明对应变量, 那么不会分配内存;



结构体变量初始化 : 

-- 声明结构体的时候初始化 : struct student s1 = {"Tom", 12} ; 注意 初值表中必须时结构体对应类型的常量表达式;

-- 声明之后初始化 : 结构体变量名.成员名 可以访问结构体中的成员变量, s1.name = "Tom"; s2.age = 12;


结构体嵌套 : 结构体中的成员变量可以是 结构体变量;

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct student  
  2. {  
  3.     char *name;  
  4.     int age;  
  5. } s1;  
  6.   
  7. struct class  
  8. {  
  9.     struct student s1;  
  10.     struct student s2;  
  11. } c1;  


结构体代码示例 : 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /************************************************************************* 
  2.     > File Name: base_struct.c 
  3.     > Author: octopus 
  4.     > Mail: octopus_work.163.com  
  5.     > Created Time: 2014年03月24日 星期一 10时49分46秒 
  6.  ************************************************************************/  
  7.   
  8. #include<stdio.h>  
  9.   
  10. int main(int argc, char **argv)  
  11. {  
  12.     /* 
  13.      * 声明结构体 同时声明变量s1 
  14.      */  
  15.     struct student  
  16.     {  
  17.         char *name;  
  18.         int age;  
  19.     } s1;  
  20.   
  21.     /* 
  22.      * 结构体嵌套 
  23.      */  
  24.     struct class  
  25.     {  
  26.         struct student s1;  
  27.         struct student s2;  
  28.     } c1;  
  29.   
  30.     struct student s2 = {"Tom", 12};/*只有声明的时候才能对结构体初始化才能使用花括号赋值*/  
  31.     struct class c2 = {{"Jack", 13}, {"Pig", 15}};  
  32.     s1.name = "Hack";       /*变量声明后对结构体赋值只能一个一个赋值*/  
  33.     s1.age = 14;  
  34.     //s1 = {"fuck", 1};     /*只有在初始化的时候才能使用 花括号初始化结构体变量*/  
  35.     c1.s1.name = "CJ";  
  36.     c1.s1.age = 21;  
  37.     c1.s2.name = "KW";  
  38.     c1.s2.age = 22;  
  39.   
  40.     /*访问结构体中的变量, 使用 . 进行访问*/  
  41.   
  42.     printf("s1 : name = %s, age = %d \n", s1.name, s1.age);  
  43.     printf("s2 : name = %s, age = %d \n", s2.name, s2.age);  
  44.     printf("c1 : s1 : name = %s, age = %d ; s2 : name = %s, age = %d \n", c1.s1.name, c1.s1.age, c1.s1.name, c1.s2.age);  
  45.     printf("c2 : s1 : name = %s, age = %d ; s2 : name = %s, age = %d \n", c2.s1.name, c2.s1.age, c2.s1.name, c2.s2.age);  
  46.   
  47.     return 0;  
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#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;i<6;i++) { scanf("%d",&temp2); p->score[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;i<6;i++) { scanf("%d",&score); q->score[i]=score; } q->next=NULL; p=head; while(p->next->num<q->num&&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;i<6;i++) printf("\t%d",p->score[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;i<6;i++) { scanf("%d",&score[i]); p->score[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、付费专栏及课程。

余额充值