本文为C语言结构的应用总结,C语言没有类的概念,但可以通过结构体构建一个类似类的操作概念。下文是使用的场景代码,由于水平有限,错误之处请各位大神指出。在此抛砖引玉,希望做嵌入式的的人能提供更多的应用的典型案例。
#include#include/*结构体定义 */
//只有结构类型的定义
struct stuff
{
char job[20];
int age;
float height;
};
//含有结构体类型的结构体变量初始化定义
struct stuff1
{
char job[20];
int age;
float height;
}Hu;
//struct stuff1 Hu //就相当于这样子
// 结构体不能再用来定义同类型的结构体变量,一锤子买卖;
struct
{
char job[20];
int age;
float height;
}Hu1;//这种类型的结构体变量只能有这么一个,不能去定义其他的
/*结构体别名声明 */
typedef struct Stu
{
char job[20];
int age;
float height;
}Stuff,*PtrStuff;
Stuff Sam;//等于 struct Stu Sam;
PtrStuff pSam;//等于 struct Stu *Sam;
/*结构体与其他类型 */
typedef struct arraychange
{
int cnt;
char pc[0];
}arraychange;//pc数组只能放在最后,不能调换位置;柔性数组,少用;
struct check
{
union
{
char a;
};
int i;
}CheckSystem; //系统内存大小端测试,结构体与联合体结合
struct structureA
{
struct structureB
{
int c;
}b; //必须要有实体才能够在后面使用struct structureB bb;
struct structureB bb;
}a;//结构体中含有结构体;
//函数可以用结构体元素,结构体,结构体指针做参数;用元素与普通没什么区别,用结构体则拷贝副本,指针是唯一的;函数可以返回结构体,使用指针的效率高
void func1(int par); //结构体元素
Stuff func2(Stuff par); //结构体,返回结构体
void func3(PtrStuff par); //结构体指针
void print(Stuff par);
//结构体含有函数指针做元素,这样就可以实现类似面向对象的类封装
int fun1(int age,int weight);
void fun2(int age,int weight);
struct Fun
{
int age;
int weight;
int (*pfun1)(int age,int weight); //函数还有一个int返回值,两个int参数
void (*pfun2)(int age,int weight); //函数没有返回值,两个int参数
};
int main(void)
{
/*结构体作为函数参数 */
Stuff test,tmptest;
PtrStuff ptest=&test;
/*结构体含有函数指针*/
struct Fun myfun={10,45,fun1,fun2};
struct Fun *ptrmyfun=&myfun;
int age=22,weight=60,temp=0;
/*结构体作为函数参数 */
strcpy(test.job,"manager");
test.age=25;
test.height=1.65;
func1(test.age);
tmptest=func2(test);
print(tmptest);
func3(ptest);
print(test);
/*结构体含有函数指针*/
temp=myfun.pfun1(age,weight); //成员调用函数
printf("temp %d",temp);
myfun.pfun2(age,weight); //成员调用函数
myfun.age=80;
myfun.weight=65;
temp=ptrmyfun->pfun1(myfun.age,myfun.weight); //指针调用函数
printf("temp %d",temp);
ptrmyfun->pfun2(myfun.age,myfun.weight); //指针调用函数
return 0;
}
void print(Stuff par)
{
printf("your job is:%s",par.job);
printf("your age is:%d",par.age);
printf("your height is:%f",par.height);
}
void func1(int par)
{
printf("your age is:%d",par);
}
Stuff func2(Stuff par)
{
strcpy(par.job,"Engineer");
par.age=30;
par.height=1.80;
return par;
}
void func3(PtrStuff par)
{
strcpy(par->job,"Programmer");
par->age=19;
par->height=170;
}
int fun1(int age,int weight)
{
int temp=0;
temp=age+weight;
return temp;
}
void fun2(int age,int weight)
{
printf("your age is:%d",age);
printf("your weight is:%d",weight);
}
C语言结构体的全面应用
发表于:2017-08-08
作者:红木娄
来源:
 相关文章
C 语言编译器(IDE)初学者指南:选... 关于编译代码,你应该知道的 撼不动的C语言! 十个超赞的C语言开源项目,强烈推荐! C语言零基础教程之C语言常量与变量,... Python与C语言的区别是什么?- 周排行
- 月排行
- 评论排行
-   高效IT:如何利用零代码开发平台快速...
-   生成式人工智能在软件开发过程现代化...
-   一体化好好的,为什么要前后端分离?
-   13种最佳App设计不止有设计美感,更跟...
-   12张图带你彻底理解分布式事务产生的...
-   我在 2023 年离不开的七个 JavaScript 必备工具
-   2023 年 Java 发展趋势
-   系统设计 | 应用、微服务、流程、规则编排
-   值得一试的五大AI编程助手
-   高效IT:如何利用零代码开发平台快速...
-   生成式人工智能在软件开发过程现代化...
-   一体化好好的,为什么要前后端分离?
-   衡量开发人员工作效率的五个技巧
-   2018年最全的App设计资源(工具+模板...
-   从软件出发,非功能测试思考总结
-   展望2017,谁是网络技术头牌?
-   C/C++单元测试工具Visual Unit 4 介绍
-   国内外最好用的6款Bug跟踪管理系统
-   性能测试指标评估必读
-   百度软件测试方案模板
-   38张史上最全的IT工程师技能图谱
-   C/C++单元测试工具Visual Unit 4 介绍
-   2019年,你会选哪些安全测试工具?
-   十款Web服务器性能压力测试工具
-   软件测试入门指南:周期、模型和文档化
-   记自动化测试成神学习之路
-   软件测试全景图 -最全的思维导图