当前位置:网站首页>详细讲解C语言12(C语言系列)
详细讲解C语言12(C语言系列)
2022-07-28 19:21:00 【奶油酒窝*٩(ˊωˋ*)و*】
目录
前言:
这次小编与大家一起来探讨一下C语言中的结构体,这节中小编只是简单使得与大家聊一下结构体,更详细的讲解还请见后期中小编与大家的分享。
结构体的概念:
在小编看来结构体就是一些值的集合,但是值的类型可以不同。
结构体的语法结构:

如何调用结构体:
当在主函数中创建对象的时候调用结构体,如下图所示。

结构体与对象的关系:
结构体类型就如同我们生活中的图纸,而当我们在主函数中创建一个结构体对象的时候,我们就可以把它看成是我们的房子,当我们要建造一座房子的时候那么房子里面的内部结构就需要图纸来展示给我们。同理当我们在创建一个结构体变量的时候,我们就需要知道这个对象的具体类型有哪些。(如下图所示)

结构体成员的访问:
. 访问和 -> 访问
具体看下列代码所示:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
struct B
{
char c;
short s;
double d;
};
struct stu
{
struct B sb;
char name[20];
int age;
char id[20];
};
int main()
{
struct stu s = { {'w',20,3.14},"张三",30,"202220034" };
//采用“.”访问
printf("%c\n", s.sb.c);
//采用“->”访问
printf("%s\n", s.id);
//当使用指针访问时
struct stu* ps = &s;
//采用“.”访问
printf("%c\n", (*ps).sb.c);
//采用“->”访问
printf("%s\n", ps->id);
return 0;
}代码运行结果如下所示:
结构体传参:
传值调用:
代码如下所示:
#define _CRT_SECURE_NO_WARNINGS 1
//传值调用
#include<stdio.h>
struct B
{
char c;
short s;
double d;
};
struct stu
{
struct B sb;
char name[20];
int age;
char id[20];
};
void print1(struct stu t)
{
printf("%c %d %lf %s %d %s\n", t.sb.c, t.sb.s, t.sb.d, t.name, t.age, t.id);
}
int main()
{
struct stu s = { {'w',20,3.14},"张三",30,"202220034" };
print1(s);//传值调用
return 0;
}代码截图如下所示:

缺点:传值调用的时候浪费了大部分空间。
传址调用:
代码如下所示:
#define _CRT_SECURE_NO_WARNINGS 1
//传址调用
#include<stdio.h>
struct B
{
char c;
short s;
double d;
};
struct stu
{
struct B sb;
char name[20];
int age;
char id[20];
};
void print2(struct stu* ps)
{
printf("%c %d %lf %s %d %s\n", ps->sb.c, ps->sb.s, ps->sb.d, ps->name, ps->age, ps->id);
}
int main()
{
struct stu s = { {'w',20,3.14},"张三",30,"202220034" };
print2(&s);//传址调用
return 0;
}代码截图如下所示:

优点:传参效率更高,而且可以改变原本的值。
选择传址调用的原因:
函数传参的时候,参数是需要压栈的。如果传递一个结构体对象的时候,结构体过大,参数压栈系统开销会比较大,所以会导致性能下降。
结论:
结构体传参的时候,传过结构体的地址。
结束语:
本次小编主要与大家分享了结构体方面一小部分知识,关注小编,小编带你一起继续探索后面的更深层次的知识哦,记得点赞要不然下次就找不到啦!希望对大家有所帮助,如果文章中有任何错误也欢迎各位大佬及时为小编指点迷津(在此小编先谢过各位大佬啦!)
边栏推荐
- C # basic 5-asynchronous
- The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
- Dom4J的Bug
- MobileViT:挑战MobileNet端侧霸主
- Ctfshow network lost track record (2)
- BUUCTF做题Upload-Labs记录pass-11~pass-20
- [C language brush questions] explanation of linked list application
- Deit: attention can also be distilled
- 【周周有奖】云原生编程挑战赛“边缘容器”赛道邀你来战!
- source insight 使用快捷键
猜你喜欢

Deit: attention can also be distilled

Unity foundation 2 editor expansion

【TiDB】txt文档导入数据库,这样做真的很高效

MySQL sorts out the review content -- with mind map

It is not only convenient, safe + intelligent, but also beautiful. Fluorite releases the Big Dipper face lock dl30f and Aurora face video lock y3000fv

Applet container technology improves mobile R & D efficiency by 500%

Space shooting Lesson 13: explosion effect

SharkTeam完成Flow生态NFT市场MatrixMarket的安全审计

Explain prefabrication in unity in detail

Job CE
随机推荐
Space shooting Lesson 15: props
Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
Explain in detail the rays and radiographic testing in unity
The average altitude is 4000 meters! We built a cloud on the roof of the world
ZCMU--5066: 黑暗长廊
Explain mesh Collider in unity
[input ID number] is replaced by an asterisk, and input is cut into multiple small squares (similar)
How does lazada store make up orders efficiently? (detailed technical explanation of evaluation self-supporting number)
【input 身份证号】星号 代替,input 切割成 多个 小格格(类似)
source insight 使用快捷键
PostgreSQL数据库删库前是不是需要把所有连接断开才能删除?
What is ci/cd| Achieve faster and better software delivery
The development of smart home industry pays close attention to edge computing and applet container technology
Color finder actual combat (QT including source code)
C language final review questions
C # basic 3-value type and reference type, packing and unpacking
Deit: attention can also be distilled
MoCo V3:视觉自监督迎来Transformer
DeiT:注意力Attention也能蒸馏
58岁安徽人,干出瑞士今年最大IPO 投资界