当前位置:网站首页>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 本地程序启动缓慢问题排查

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

Push down of spark filter operator on parquet file

/Dev/loop1 takes up 100% of the problem

【剑指offer】面试题49:丑数

Pictures to be delivered
![[daily question 1] 558. Intersection of quadtrees](/img/96/16ec3031161a2efdb4ac69b882a681.png)
[daily question 1] 558. Intersection of quadtrees

Fluent -- layout principle and constraints

初探JuiceFS

flutter —— 布局原理与约束
随机推荐
Spark TroubleShooting整理
flutter —— 布局原理与约束
表格插入行内公式后,单元格失去焦点
IP protocol of network layer
【剑指offer】面试题42:连续子数组的最大和——附0x80000000与INT_MIN
C语言中交换两数的方法
Learn parquet file format
“router-link”各种属性解释
Go language slow start - package
Use deconstruction to exchange the values of two variables
Singles cup, web:web check in
C语言:扫雷小游戏
[0 basic operations research] [super detail] column generation
STL value string learning
使用Prometheus监控Spark任务
Network equipment hard core technology insider router Chapter 21 reconfigurable router
Causes and solutions of deadlock in threads
Record record record
【云享读书会第13期】音频文件的封装格式和编码格式
[daily question 1] 558. Intersection of quadtrees