当前位置:网站首页>First understanding of structure
First understanding of structure
2022-07-27 15:45:00 【FA FA is a silly goose】
It may be a little cumbersome , But please read it patiently ~~
Before talking about what a structure is , Let's talk about why there is a structure , stay C In language , If we want to create an integer variable , Then we can use int, Want to create a character variable , It can be used char, Want to create a decimal , It can be used float perhaps double, But if we want to describe a person , Or a Book , What to do ? Take people for example , Describe a person , Need his name 、 Gender 、 Age and so on , These variables are not of the same type , So we can't use simple int perhaps double Wait for keywords to create , So we need a variable type that can meet various types of variables , So the structure comes, but this important task .
So what is a structure ? A structure is a collection of values , These values are called member variables . Each member of a structure can be a variable of a different type . Its members can be said to be scalars 、 Array 、 The pointer 、 Even another structure .
1. Declaration of a structure
Before using a structure , Need statement , The format is as follows :
struct stu
{
char name[20];
char gender[10];
int age;
};
int main()
{
struct stu s = {
"hahaha","man",20 };
return 0;
}
among struct Keyword ,stu Is the name of the structure ,name、gender and age Is a member variable of the structure ,main The structure variable is defined in the function s And give it an initial value .
2. There are also two types
struct stu //1
{
char name[20];
char gender[10];
int age;
}s1;
typedef struct stu //2
{
char name[20];
char gender[10];
int age;
}stu;
stu s2 = {
"lalala","women",18 };
form 1 After parentheses s1 It's a variable. , Similar to the previous variables s, Can be created directly , But it still needs to be assigned ;
form 2 We should ensure two stu Exactly the same as , Because this is the format requirement , And you can't create variables at the end , But there is no need to add keywords when creating variables in this form struct.
3. Access member variables
When we need to access member variables , Need to use .( spot ) This symbol , The access format is as follows :
printf("%s ", s.name);
printf("%s ", s.gender);
printf("%d ", s.age);
4. Structural parameters
We know that the forms of parameter transmission include value transmission and address transmission , Structure is no exception , Go straight to the code :
void print1(struct stu s)
{
printf("%s\n", s.name);
}
void print2(struct stu* s)
{
printf("%s\n", s->name);
}
int main()
{
struct stu s = {
"hahaha","man",20 };
print1(s);
print2(&s);
return 0;
}

These two parameter transfer methods can print the results well , Careful people will find that the way of accessing member variables by passing values and addresses is different , For Value Passing Access .( spot ) This symbol , For address access ->, besides , Byref (&s) Compare and transfer values (s) More space saving , Value passing is the size of the whole structure passed , And value passing is to pass the address of the structure , The size of the address is 4 or 8 Bytes , If there are many members in the structure , Address transmission will save more space .
边栏推荐
- 【剑指offer】面试题55 - Ⅰ/Ⅱ:二叉树的深度/平衡二叉树
- 《吐血整理》C#一些常用的帮助类
- Record record record
- shell脚本读取文本中的redis命令批量插入redis
- 股票开户佣金优惠,炒股开户哪家证券公司好网上开户安全吗
- [系统编程] 进程,线程问题总结
- 【剑指offer】面试题52:两个链表的第一个公共节点——栈、哈希表、双指针
- CAS比较交换的知识、ABA问题、锁升级的流程
- Extended log4j supports the automatic deletion of log files according to time division and expired files
- 复杂度分析
猜你喜欢

STL value string learning

【剑指offer】面试题42:连续子数组的最大和——附0x80000000与INT_MIN

【剑指offer】面试题50:第一个只出现一次的字符——哈希表查找

Voice live broadcast system -- a necessary means to improve the security of cloud storage

折半插入排序

Record record record

npm install错误 unable to access

Dan bin Investment Summit: on the importance of asset management!

Spark Bucket Table Join

Pictures to be delivered
随机推荐
Using Prometheus to monitor spark tasks
Unity3d learning note 10 - texture array
Binder初始化过程
It is said that the US government will issue sales licenses to Huawei to some US enterprises!
leetcode-1:两数之和
Implementation of spark lazy list files
Jump to the specified position when video continues playing
复杂度分析
传美国政府将向部分美企发放对华为销售许可证!
Learn parquet file format
[正则表达式] 单个字符匹配
go语言慢速入门——基本内置类型
js操作dom节点
Spark 3.0 Adaptive Execution 代码实现及数据倾斜优化
一文读懂鼠标滚轮事件(wheelEvent)
折半插入排序
兆骑科创创业大赛策划承办机构,双创平台,项目落地对接
Spark RPC
Network equipment hard core technology insider router 19 dpdk (IV)
Pictures to be delivered