当前位置:网站首页>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 .
边栏推荐
猜你喜欢
随机推荐
使用Prometheus监控Spark任务
Spark 3.0 DPP implementation logic
Fluent -- layout principle and constraints
How to take satisfactory photos / videos from hololens
Use double stars instead of math.pow()
传美国政府将向部分美企发放对华为销售许可证!
C语言:动态内存函数
Spark TroubleShooting整理
js使用for in和for of来简化普通for循环
Jump to the specified position when video continues playing
【剑指offer】面试题49:丑数
【剑指offer】面试题51:数组中的逆序对——归并排序
Explanation of various attributes of "router link"
synchronized和ReentrantLock的区别
Spark Bucket Table Join
表格插入行内公式后,单元格失去焦点
NPM install error unable to access
$router.back(-1)
Interview focus - TCP protocol of transport layer
Implement custom spark optimization rules

![[0 basic operations research] [super detail] column generation](/img/cd/f2521824c9ef6a50ec2be307c584ca.png)







