这里写目录标题

  • 结构体变量的引用
  • 机构体变量的初始化
  • 结构体数组
    • 例子
  • 结构指针作为函数参数
    • 结构体变量作为函数参数
    • 指向结构体变量的指针作为实参
  • 动态存储分配
  • 动态存储分配
  • 链表
    • 对链表的删除操作
  • 对链表进行插入操作
  • typedef
  • typedef指向函数的指针
  • 共用体
  • 枚举类型







结构体变量的引用






机构体变量的初始化


结构体数组

例子

通讯录



#include <stdio.h>
#include <string.h>#define I 3struct vote
{char name[20];int num;
};void main(){struct vote leader[I]={"张三",0,"李四",0,"王小二",0};char leader_name[20];int i,j;for(i=1;i<=10;i++){printf("请输入第%d位投票人员: ",i);scanf("%s",&leader_name);for(j=0;j<I;j++){if(strcmp(leader_name,leader[j].name)==0){leader[j].num++;}}}printf("投票结果: \n");for(i=0;i<3;i++){printf("%s 的票数为 %d\n",leader[i].name,leader[i].num);}
}


`






结构指针作为函数参数

结构体变量作为函数参数

指向结构体变量的指针作为实参

动态存储分配

动态存储分配



链表














#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>#define LEN sizeof(struct student) // student结构的大小struct student *creat();       //创建链表
void print(struct student *head); //打印链表struct student
{int num;float score;struct student *next;
};int n; //全局变量,用来记录存放了多少数据。void main()
{struct student *stu;stu = creat();print( stu );printf("\n\n");system("pause");
}struct student *creat()
{struct student *head;struct student *p1, *p2;p1 = p2 = (struct student *)malloc(LEN); //LEN是student结构的大小printf("Please enter the num :");scanf("%d",&p1->num);printf("Please enter the score :");scanf("%f",&p1->score);head = NULL;n = 0;while( p1->num ){n++;if( 1==n ){head = p1;}else{p2->next = p1;}p2 = p1;p1 = (struct student *)malloc(LEN);printf("\nPlease enter the num :");scanf("%d",&p1->num);printf("Please enter the score :"); scanf("%f",&p1->score);}p2->next = NULL;return head;
}void print(struct student *head)
{struct student *p;printf("\nThere are %d records!\n",n);p = head;if( head )   /* 也可以写成 NULL != head */{do{printf("学号为 %d 的成绩是: %f\n",p->num,p->score);p = p->next;}while( p );}
}


对链表的删除操作



#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>#define LEN sizeof(struct student) // student结构的大小struct student *creat();       //创建链表
struct student *del( struct student *head,int num); // del函数用于删除节点, *head即链表// 的头指针, num是要删除的节点num。
void print(struct student *head); //打印链表struct student
{int num;float score;struct student *next;
};int n; //全局变量,用来记录存放了多少数据。void main()
{struct student *stu;stu = creat();print( stu );printf("\n\n");system("pause");
}struct student *creat()
{struct student *head;struct student *p1, *p2;p1 = p2 = (struct student *)malloc(LEN); //LEN是student结构的大小printf("Please enter the num :");scanf("%d",&p1->num);printf("Please enter the score :");scanf("%f",&p1->score);head = NULL;n = 0;while( p1->num ){n++;if( 1==n ){head = p1;}else{p2->next = p1;}p2 = p1;p1 = (struct student *)malloc(LEN);printf("\nPlease enter the num :");scanf("%d",&p1->num);printf("Please enter the score :"); scanf("%f",&p1->score);}p2->next = NULL;return head;
}void print(struct student *head)
{struct student *p;printf("\nThere are %d records!\n",n);p = head;if( head ){do{printf("学号为 %d 的成绩是: %f\n",p->num,p->score);p = p->next;}while( p );}
}struct student *del( struct student *head,int num)
{struct student *p1,*p2;if( NULL == head ){printf("\nThis list is null!\n");goto end;}p1 = head;while(p1->num != num && p1->next != NULL){p2 = p1;p1 = p1->next;}if( num == p1->num ){if( p1 == head ) // 当将要删除的结点位于头结点的时候{head = p1->next;}else // 一般情况{p2->next = p1->next;}printf("\nDelete No: %d successd!\n",num);n = n-1;}else{printf("%d not been found!\n",num);}
end :return head;
}

对链表进行插入操作

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>#define LEN sizeof(struct student)struct student
{long num;float score;struct student *next;
};int n;struct student * creat(void)
{struct student *head;struct student *p1,*p2;head = NULL;n=0;p1 = p2 = (struct student *)malloc(LEN);scanf("%ld,%f",&p1->num,&p1->score);while(p1->num != 0){n=n+1;if(n==1){head = p1;}else{p2->next = p1;}p2 = p1;p1 = (struct student *)malloc(LEN);scanf("%ld,%f",&p1->num,&p1->score);}p2->next = NULL;return head;
}struct student * create(struct student *head,int num)
{struct student *p1,*p2,*p3,*hcr;p1=p2=head;int s=0;hcr = NULL;while(p1 != NULL){s = s+1;if(s==1){         hcr = p1;}elseif(s!=num){p2->next = p1;           }else{printf("请输入你要插入的数据: ");p3 = (struct student *)malloc(LEN);scanf("%ld,%f",&p3->num,&p3->score);p3->next = p1;p2 ->next = p3;}if(s!=num){p2 = p1;} else {p3 = p1;p2 = p3;}p1 = p1->next;}p2->next = NULL;return hcr;
}void main(){struct student *pt,*pcr,*t;pt=creat();t=pt;do{printf("%ld,%f\n",pt->num,pt->score);pt = pt->next;}while(pt != NULL);printf("\n");printf("请输入你要插入的链位置: ");int num;scanf("%d",&num);pcr = create(t,num);do{printf("%ld,%f\n",pcr->num,pcr->score);pcr = pcr->next;}while(pcr != NULL);}

typedef


typedef指向函数的指针





共用体







#include <stdio.h>
#include <string.h>struct
{int num;char name[10];char sex[10];char job;union{int banji;char position[10];}category;
}person[2];void main()
{int i;for(i=0;i<2;i++){printf("please input num: ");scanf("%d",&person[i].num);printf("please input name: ");scanf("%s",&person[i].name);printf("you sex is M|G: ");scanf("%s",&person[i].sex);printf("you job is s|t: ");scanf("%s",&person[i].job);if(person[i].job=='s'){printf("please input baiji num: ");scanf("%d",&person[i].category.banji);}else{printf("please input position: ");scanf("%s",&person[i].category.position);}printf("\n\n");}for(i=0;i<2;i++){printf("\tnum\tname\tsex\tjob\tbanji/position\n");if(person[i].job=='s'){printf("\t%d\t%s\t%s\t%c\t%d\n",person[i].num,person[i].name,person[i].sex,person[i].job,person[i].category.banji);}else{printf("\t%d\t%s\t%s\t%c\t%s\n",person[i].num,person[i].name,person[i].sex,person[i].job,person[i].category.position);}}
}

枚举类型





#include <stdio.h>void main()
{enum boy{Tom,Danny,Gan,LiLei};enum boy month[31],j;int i;j = Tom;for(i=1;i<=30;i++){month[i] = j;j=boy(j+1);if(j > LiLei){j = Tom;}}for(i=1;i<=30;i++){switch(month[i]){case Tom: printf(" %4d  %s\t",i,"Tom");break;case Danny: printf(" %4d  %s\t",i,"Danny");break;case Gan: printf(" %4d  %s\t",i,"Gan");break;case LiLei: printf(" %4d  %s\t",i,"LiLei");break;default:break;}if(i%5==0){printf("\n");} }printf("\n");}

C语言之结构体与共同体相关推荐

  1. c语言 结构体 ppt,第8章C语言的结构体和共同体.ppt

    <第8章C语言的结构体和共同体.ppt>由会员分享,可在线阅读,更多相关<第8章C语言的结构体和共同体.ppt(41页珍藏版)>请在皮匠网上搜索. 1.1第八章结构体.共用体和 ...

  2. C语言 | 10 结构体与共同体

    10.1 用typedef 说明一个新类型 typedef为C语言的关键字,作用是为一种数据类型(基本类型或自定义数据类型)定义一个新名字,不能创建新类型. 与#define不同,typedef仅限于 ...

  3. C语言之结构体和共同体——结构体的定义和使用

    在实际问题中,一组数据往往具有不同的数据类型:例如在学生信息登记表中,姓名为字符型,学号为整型或字符型,年龄为整型,性别为字符型,成绩为整型或实型.因为数据类型不同,显然不能用一个数组来存放. 在C语 ...

  4. c语言结构体与共同体课件,《结构体与共同体》PPT课件.ppt

    <<结构体与共同体>PPT课件.ppt>由会员分享,可在线阅读,更多相关<<结构体与共同体>PPT课件.ppt(44页珍藏版)>请在装配图网上搜索. 1 ...

  5. 结构体与共同体(联合体)的妙用

    结构体与共同体(联合体)的妙用 学习过C语言后,大家都了解了结构体与共同体.两者之间的区别是: 共同体:使几个不同类型的变量共占一段内存(相互覆盖).所占内存长度是各最长的成员占的内存长度. 结构体: ...

  6. C 语言编程 — 结构体的数据类型转换

    目录 文章目录 目录 数组类型强制类型转换为结构体 结构体之间的强制类型转换 数组类型强制类型转换为结构体 先看一个例子: #include <stdio.h>int main(void) ...

  7. 【C 语言】结构体 ( 结构体 数组 作为函数参数 | 数组 在 堆内存创建 )

    文章目录 一.结构体 数组 作为函数参数 ( 数组 在 堆内存创建 ) 二.完整代码示例 一.结构体 数组 作为函数参数 ( 数组 在 堆内存创建 ) 在上一篇博客 [C 语言]结构体 ( 结构体 数 ...

  8. 【剑仙教程】易语言的结构体。自定义数据类型。

    1楼. [剑仙教程]易语言的结构体.自定义数据类型. 在易语言中,有基本数据类型,自定义数据类型. 自定义数据类型,在C语言/C++中称为结构体.这是面向对象和类的编程. . . 先上代码.打开易语言 ...

  9. C 语言中结构体强制转换--实验

    2019独角兽企业重金招聘Python工程师标准>>> 对于C语言中结构体强制转换做了如下实验, 或许可以解惑一些问题 对于结构体, 我理解的属性有: 成员的顺序, 成员的类型,成员 ...

最新文章

  1. configure.ac:64: error: possibly undefined macro: AM_ICONV
  2. Spring boot修改静态资源映射
  3. 【JUC并发编程10】阻塞队列
  4. 深入浅出学java_《深入浅出学JAVA开发初级》
  5. 【英语学习】【English L06】U06 Banking L1 How can I save money?
  6. English Voice of I Knew You Were Trouble.
  7. 在pytorch中使用tensorboard
  8. 关于编码问题,报错:'gbk' codec can't encode character '\u3164' in position 0: illegal multibyte sequence...
  9. 【渝粤教育】国家开放大学2019年春季 1067知识产权法 参考试题
  10. 牛腩新闻发布系统(7)——总结
  11. 离散数学及其应用【华章版】习题答案第一章01
  12. 如何做好软件开发项目的需求分析
  13. Windows ToolTips简要介绍
  14. Go的编译原理系列二之编译过程
  15. C语言经典案例——第六章 字符串
  16. 如何接触的最新的前端动态、最前沿的前端技术
  17. Python深度学习婴儿啼哭声分类识别,测试集准确率67.5%
  18. 全球创业新趋势:私域流量+DTC,蜂巢跨境私域课堂
  19. easyExcel导入excel实现动态进度条
  20. GScan:Linux Checklist自动化检测

热门文章

  1. Source Insight 4 全屏问题
  2. 为什么32 >> 32等于32
  3. C语言求最大公约数和最小公倍数(思路清晰+拓展)
  4. java weakhashmap_Java WeakHashMap类
  5. 解决cocos creator微信小游戏本地缓存文件超过限制问题
  6. 亲爱的行业佼佼者您好
  7. PlayBook模拟器的安装与设置
  8. 2008年北京奥运会赛程表—— 08-19
  9. 计算机毕业设计和毕业论文
  10. 北邮 计算机组成原理