当前位置:网站首页>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";
边栏推荐
- What is the difference between server leasing and hosting?
- Use lightweight application server to automatically download and upload to onedrive
- Black hat SEO actual combat search engine snapshot hijacking
- MySQL stored procedure + function
- Differences between EDI and VMI
- How to gracefully handle and return errors in go (1) -- error handling inside functions
- NLP task summary introduction and understanding
- 内存泄漏之KOOM-Shark中的Hprof信息
- Independent innovation and localization technology: SMT production line monitoring and management visualization of intelligent manufacturing
- Psexec right raising
猜你喜欢

在pycharm中pytorch的安装

Brief ideas and simple cases of JVM tuning - how to tune

【代码随想录-动态规划】T392.判断子序列

Black hat SEO practice: General 301 weight PR hijacking

黑帽SEO实战之通用301权重pr劫持

ClickHouse(02)ClickHouse架构设计介绍概述与ClickHouse数据分片设计

Black hat SEO actual combat search engine snapshot hijacking

Ar 3D map technology

黑帽SEO实战之目录轮链批量生成百万页面

618 promotion: mobile phone brand "immortal fight", high-end market "who dominates the ups and downs"?
随机推荐
How to select the application of the server?
Installation of pytorch in pycharm
The collection method of penetration test, and which methods can be used to find the real IP
Rasa 3.x 学习系列-Rasa 3.2.0 新版本发布
Interpreting Tencent cloud product experience through user experience elements
[congratulations] rock solid! A new generation of AMD Blackstone architecture instance is launched!
There is such a shortcut to learn a programming language systematically
Record a programming contest
What should I pay attention to when choosing a data center?
Independent innovation and localization technology: SMT production line monitoring and management visualization of intelligent manufacturing
golang clean a slice
Several options of F8 are very useful
How to handle the uplink and downlink silence of TRTC
Tencent cloud console work order submission Guide
一次 MySQL 误操作导致的事故,「高可用」都顶不住了!
Old popup explorer Exe has stopped working due to problems. What should I do?
Actual battle case | refuse information disclosure, Tencent cloud helps e-commerce fight against web crawlers
Koom of memory leak
Record the creation process of a joke widget (II)
黑帽SEO实战搜索引擎快照劫持