当前位置:网站首页>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 .
边栏推荐
- Spark Bucket Table Join
- [正则表达式] 匹配多个字符
- Interview focus - TCP protocol of transport layer
- Pictures to be delivered
- 【剑指offer】面试题52:两个链表的第一个公共节点——栈、哈希表、双指针
- /dev/loop1占用100%问题
- 文字批量替换功能
- Multi table query_ Exercise 1 & Exercise 2 & Exercise 3
- Network equipment hard core technology insider router 20 dpdk (V)
- $router.back(-1)
猜你喜欢

How to take satisfactory photos / videos from hololens

复杂度分析
![[daily question 1] 558. Intersection of quadtrees](/img/96/16ec3031161a2efdb4ac69b882a681.png)
[daily question 1] 558. Intersection of quadtrees

The difference between synchronized and reentrantlock

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

Singles cup, web:web check in

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

【剑指offer】面试题41:数据流中的中位数——大、小堆实现

Spark 3.0 Adaptive Execution 代码实现及数据倾斜优化

IP protocol of network layer
随机推荐
Spark lazy list files 的实现
Unity3d learning note 10 - texture array
【剑指offer】面试题51:数组中的逆序对——归并排序
【剑指offer】面试题56-Ⅰ:数组中数字出现的次数Ⅰ
Multi table query_ Exercise 1 & Exercise 2 & Exercise 3
Binder initialization process
[正则表达式] 匹配多个字符
文字批量替换功能
Hyperlink parsing in MD: parsing `this$ Set() `, ` $` should be preceded by a space or escape character`\`
STL value string learning
Singles cup, web:web check in
Zhaoqi scientific innovation and entrepreneurship competition planning and undertaking organization, mass entrepreneurship and innovation platform, project landing and docking
HJ8 合并表记录
Spark troubleshooting finishing
Learn parquet file format
Text batch replacement function
Explanation of various attributes of "router link"
Spark Bucket Table Join
【剑指offer】面试题49:丑数
Using Prometheus to monitor spark tasks