当前位置:网站首页>Understanding of structure in C language
Understanding of structure in C language
2022-06-24 03:55:00 【Good pie notes】
1, What is the structure
A structure is a type of composite data , Its member type can be an interrelated basic data type or a structure type
2, Use occasion
When dealing with a large number of different data types that are related , For example, a student's information , Student number (int), full name (char *), Age (int) Etc , You can use the structure , Greatly improved efficiency
3, How to use it?
(1) Declare or define the structure type
Different from the definition of basic data variables , Because the system already knows the structure of basic data types , Type name Variable name
You can do it , But the structure is defined by the user ,
The first step is to declare or define the structure type , Tell the system that there is this type , What is it like , The way to define the structure type is : Use keywords struct Followed by type name
struct Type name
{
Member list ;// A member class table is a member type name ; A type can be a basic data type , It can also be a structure type
}
Such as :struct Student
{
int num;
char name[20];
char sex;
int age;
char address[20];
}; // This defines a structure type Student, It contains all kinds of information about students
(2) Define structure variables
When declaring a structure type ( It's like a template ) After that, you can define structure variables , There are three main ways :
1> > Declaring structure types is separate from defining structure variables ( The most common and flexible way )
struct Student
{
int num;
char name[20];
char sex;
int age;
char address[20];
};// After declaring the structure type struct Student after , Define structure variables
struct Student studnet1,student2;// Two structure variables are defined student1 and student2
2>> Define structure variables while declaring structure types . Such as :
struct Student
{
int num;
char name[20];
char sex;
int age;
char address[20];
}studnet1,student2;
3>> Define structure variables while declaring structure types ( Anonymous object ), But there is no type name Such as :
struct
{
int num;
char name[20];
char sex;
int age;
char address[20];
}studnet3;
4, Use caution
1>> Structure type and structure variable are two different concepts , The structure type is equivalent to a template , At compile time
Not allocating storage space , Only for defined .
Structure variables allocate corresponding storage space according to the definition of the template , In this case, only structural variables can be assigned , save
Summation operation , The structure type cannot be assigned , Access and operation
2>> Structure type and structure variable cannot be defined repeatedly in the same scope , Except for anonymous type objects
3>> It can be inside or outside the function , The effect and usage are similar to local variables and global variables , You can also nest definitions
4>> You can define the same struct type inside and outside the function , Also follow the principle of proximity
5>> Only when the structure variable is defined can it be initialized in batch , After defining the structure variable , Only one by one
Initiation
5, The difference with array
1>> Structures can hold different types of elements , Arrays can only be of the same type
2>> The structure type needs to be defined by us . Array is to add... With other types [ Element number ]
3>> Structure memory allocation is very special , Use alignment principles , It doesn't have to be the number of bytes and , And the array must be the
There are the number of bytes of the element and .
4>> A pointer to a structure can refer to a name -> Structure element name ( Take the element ); Not an array
6, Array of structs
Structure array is essentially an array , Array elements are structural variables of the same type , For example, define a Student Structure
Array is used to store the information of all students in a class
7, Structure pointer
Is a pointer to a structure variable , Usage is as follows
struct Person
{
char *name;
int age;
}; // Define the structure type
struct Person per={"wangjintaof",2};// Define structure variables and initialize them in batches at the same time
struct Person *p=&per; // Define a pointer to a structure type , And initialization , Make it point to pe
Then you can use the pointer to reference and access the elements of the structure variable , The three methods are as follows
1>>>. P->name = "wangjintao";// to name Member variable assignment
2>>>. (*p).name = "wangjitnao";// to name Member variable assignment 、
3>>>. Per.name="wangjitnao";
边栏推荐
- Rasa 3. X learning series -rasa 3.2.0 new release
- The first 2021 Western cloud security summit is coming! See you in Xi'an on September 26!
- Mocktio usage (Part 2)
- Differences between EDI and VMI
- Clickhouse (02) Clickhouse architecture design introduction overview and Clickhouse data slicing design
- There is such a shortcut to learn a programming language systematically
- Prometheus PushGateway 碎碎念
- Using RDM (Remote Desktop Manager) to import CSV batch remote
- Build a small program + management background in 7 days, and this goose factory HR is blessed!
- How EDI changes supply chain management
猜你喜欢

ModStartCMS 主题入门开发教程

Common content of pine script script

在pycharm中pytorch的安装
![[numpy] numpy's judgment on Nan value](/img/aa/dc75a86bbb9f5a235b1baf5f3495ff.png)
[numpy] numpy's judgment on Nan value

Do you understand TLS protocol?

Black hat SEO practice: General 301 weight PR hijacking

一次 MySQL 误操作导致的事故,「高可用」都顶不住了!

An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!

SQL注入绕过安全狗思路一

黑帽SEO实战搜索引擎快照劫持
随机推荐
Rasa 3.x 学习系列-Rasa 3.2.0 新版本发布
web渗透测试----5、暴力破解漏洞--(2)SNMP密码破解
Industrial security experts talk about how to build security protection capability for government big data platform?
How to select a high-performance amd virtual machine? AWS, Google cloud, ucloud, Tencent cloud test big PK
[Numpy] Numpy对于NaN值的判断
hprofStringCache
618大促:手机品牌“神仙打架”,高端市场“谁主沉浮”?
How EDI changes supply chain management
4. go deep into tidb: detailed explanation of the implementation process of the implementation plan
General scheme for improving reading and writing ability of online es cluster
How do websites use CDN? What are the benefits of using it?
How to choose excellent server hosting or server leasing in Beijing
Protect your system with fail2ban and firewalld blacklists
LeetCode 1281. Difference of sum of bit product of integer
Event id:7001: after restarting the machine, the World Wide Web failed to start automatically, resulting in inaccessible websites
元气森林推“有矿”,农夫山泉们跟着“卷”?
How to gracefully handle and return errors in go (1) -- error handling inside functions
hprofStringCache
浅谈游戏安全 (一)
Grp: how to add Prometheus monitoring in GRP service?