当前位置:网站首页>Elementary C language - structure
Elementary C language - structure
2022-07-18 12:23:00 【Cross the boat (keep learning duck every day)】
Catalog
1.1 Basic knowledge of structure
1.2 Declaration of struct type
1.4 Creation and initialization of structure variables
1.4.1 Creation of structure variables :
1.4.2 Initialization of structure variables :
2. Access to structure members
2.1 Structure variables accessing structure members
2.2 Structure pointer to access structure members
2.3 Summary of visits of structure members
1. Declaration of a structure
1.1 Basic knowledge of 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 .
A structure is a collection . Each member in the structure can be of different types .
Analogy and distinction : An array is a collection of elements of the same type , All elements in the array are of the same type .
At present, we have learned some types : Character (char), integer (short,int...), floating-point (float,double) , These types are built-in types .
But there are always some complex objects in life , such as : people , People have names , Gender , Telephone , Height, etc ; Another example : book , Books have titles , author , pricing , Book number, etc , These complex objects cannot be described by built-in types . So how to describe these complex objects ? So there is the emergence of structure , A structure is a User defined data type , Custom type , Is used to describe complex objects .
1.2 Declaration of struct type
struct: Structure keywords , It indicates a structure , The declaration of the structure must have struct.
Structural tag : It is the name you give your structure type
struct Structural tag : This represents a structure type
Member list : You can have multiple members , Each member has its own data type and member variable name .
Structure variable list : There can be multiple variables , Is a variable created with a structure type , Generally, the list of structural variables is omitted and not written , First declare the structure , Then create structure variables .
Take an example of a structure type declaration :

The following is the case with a list of structural variables

1.3 Type of structure member
Members of a structure can be scalars (int,float,double)、 Array 、 The pointer , Even other structures ( Variable ).
such as :

1.4 Creation and initialization of structure variables
1.4.1 Creation of structure variables :
Create local variables p1:

Type does not occupy memory , Only after creating variables with types can memory be used .
Create global variables p2,p3:

1.4.2 Initialization of structure variables :
initialization : Assign values to variables while creating them .


2. Access to structure members
Structural variable . Structure member variables
Structure pointer -> Structure member variables
2.1 Structure variables accessing structure members
Structural variables pass through the dot operator (.) Access structure members .
Structural variable . Structure member variables
like this :

give an example 1:
#include <stdio.h>
// The declared structure type is :struct Peo
struct Peo
{
char name[20];
char sex[5];
char tele[12];
int high;
};
// The declared structure type is :struct S
struct S
{
struct Peo p;
int sum;
float f;
};
int main()
{
struct Peo p1 = { " Zhang San "," male ","12366784567",183 };
struct S s = { {" Li Si "," male ","12366784567",186},10,3.14f };
printf("%s %s %s %d\n",p1.name,p1.sex,p1.tele,p1.high);
printf("%s %s %s %d %d %f",s.p.name,s.p.sex,s.p.tele,s.p.high,s.sum,s.f);
return 0;
}
give an example 2: Structural parameters ( The structural variables are passed )

The code is as follows :
#include <stdio.h>
// The declared structure type is :struct Peo
struct Peo
{
char name[20];
char sex[5];
char tele[12];
int high;
};
void print(struct Peo sp)// Structure type variables to receive
{
printf("%s %s %s %d\n", sp.name, sp.sex, sp.tele, sp.high);
}
int main()
{
struct Peo p1 = { " Zhang San "," male ","12366784567",183 };
print(p1);// It is a variable of structure type
return 0;
}
2.2 Structure pointer to access structure members
Structure pointer through operator (->) Access structure members .
Structure pointer -> Structure member variables
like this :

give an example : Structural parameters ( The address of the structure variable is passed )
#include <stdio.h>
// The declared structure type is :struct Peo
struct Peo
{
char name[20];
char sex[5];
char tele[12];
int high;
};
void print(struct Peo* sp)// Structure type pointer to receive
{
printf("%s %s %s %d\n", sp->name, sp->sex, sp->tele, sp->high);
}
int main()
{
struct Peo p1 = { " Zhang San "," male ","12366784567",183 };
print(&p1);// The address of the variable name of the structure is passed , Address of structure type
return 0;
}
2.3 Summary of visits of structure members
Structural variable . Structure member variables
Structure pointer -> Structure member variables
give an example :

The code is as follows :
#include <stdio.h>
// The declared structure type is :struct Peo
struct Peo
{
char name[20];
char sex[5];
char tele[12];
int high;
};
// The declared structure type is :struct S
struct S
{
struct Peo p;
int sum;
float f;
};
void print(struct Peo* sp,struct S* ss)// Structure type pointer to receive
{
printf("%s %s %s %d\n",sp->name,sp->sex,sp->tele,sp->high);
// Structure pointer -> Structure member variables :sp->name
printf("%s %s %s %d %d %f\n",ss->p.name,ss->p.sex,ss->p.tele,ss->p.high,ss->sum,ss->f);
// Structure pointer -> Structure member variables :ss->p Structural variable . Structure member variables :p.name
}
int main()
{
struct Peo p1 = { " Zhang San "," male ","12366784567",183 };
struct S s = { {" Li Si "," male ","12366784567",186},10,3.14f };
print(&p1,&s);// The address of the variable name of the structure is passed , Address of structure type
return 0;
} 
3. Structural parameters
When structures transmit parameters , To transfer the address of the structure .
If the structural variable is passed , A formal parameter is a temporary copy of an argument , It will waste a lot of memory space .
If the address of the structure is passed , The parameter received a pointer , The formal parameter part only needs to open up a memory space of the size of the pointer variable , It only needs 4 Bytes or 8 Bytes , Reduce the waste of space .
边栏推荐
- 长安链介绍-02
- openEuler 知:管理策略
- Top100国内NFT平台 联盟链、公链使用情况统计
- 汉字风格迁移篇--基于生成对抗网络的无监督字体风格转换模型
- Xunwei Godson development board domestic dual core 64 bit loognix system dual Gigabit Ethernet more interfaces
- openEuler 知:官方社区
- Vue+mysql connects to the database to realize login and registration
- openSource 知:嵌入式软件列表
- In three steps, I finished MySQL in one day, which made me win tmall offer smoothly
- 2022年7月16日CDGA/CDGP数据治理认证考试成绩出来啦!
猜你喜欢

Luogu questionnaire - greed
![Kettle [practice 02] TXT type files are classified and imported, execute SQL to convert data types and put them into storage (complete process instance cloud resource sharing: including sql+kjb+ktr+ t](/img/db/769ef66be162b79eae4b63598ca1cf.png)
Kettle [practice 02] TXT type files are classified and imported, execute SQL to convert data types and put them into storage (complete process instance cloud resource sharing: including sql+kjb+ktr+ t

C language dynamic memory management -- just four functions

Simulation of common functions in C language

openEuler 知:ip addr 查不到 ip 的解决方法

谷歌 | 图神经网络预训练帮助了分子表征吗

Introduction to Chang'an chain-01

Basic part of C language: pointer (elementary level)

MySQL original field to hump naming

Xunwei Godson development board domestic dual core 64 bit loognix system dual Gigabit Ethernet more interfaces
随机推荐
ThinkPHP code execution (cnvd-2018-24942)
Top100国内NFT平台 联盟链、公链使用情况统计
【对象头】查看对象占用字节
279. 完全平方数
C# 使用JObject解析嵌套json
thinkphp 代码执行 (CNVD-2018-24942)
【微信小程序】基本橫向、纵向滚动Scroll-view(95/100)
【鸡汤】天下事有难易乎
Pinctrl subsystem and GPIO subsystem
【Jailhouse 文章】Bao: A Lightweight Static Partitioning Hypervisor for Modern Multi-Core Embedded...
Map set to object, map field has horizontal lines, object to map
Installing MySQL on Linux
Database system probability -- relational database
Openeuler knowledge: management strategy
JupyterLab安装
Openeuler knows: the solution of IP addr not finding IP
Overview of database system -- overview of data model
How to view RT thread documents, engineering establishment of RT and rapid construction of BSP
从应用到底层:36张图带你进入Redis世界(下)
[wechat applet] simple and easy-to-use icon (94/100)