当前位置:网站首页>C language structure
C language structure
2022-06-30 18:15:00 【*Thousands of miles of mountains and rivers*】
One 、 Structure
C Language provides basic data structure , for example char 、short 、int 、float.... Other types ; These pairs are called built-in types . How to design your own type ?
such as : When we define a person , Different attributes of people are more difficult to define with the same data type , Because of people's height 、 Age 、 Attributes such as weight often require different data types , At this time , We introduce the concept of structure
Use a structure to encapsulate some attributes , Design new types , stay C In language, it is called struct type . 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
Structure statement (structure declaration)
struct Structure name
{
Member list ( It can be a basic data type , The pointer , Array or other structure type )
};
/*struct The keyword represents a structure .*/struct People
{
char s_id[8];
char s_name[8];
char s_sex[4];
int s_age;
};Be careful ;
- keyword struct Is the data type specifier , Point out that the following is the structure type ;
- identifier Student Is the type name of the structure ;
- The last semicolon must be written ;
Two 、 Structure definition and initialization
The declaration of the structure is just a simple description , In fact, before the structure type variable is defined , It doesn't allocate space in memory .
int main()
{
struct People people;// local variable -- Put it in the stack area
return 0;
}* struct Structure name Structure variable name
A struct is a data type , In other words, it can be used to define variables . Like a “ Templates ”, The variables defined have the same properties . You can compare a structure to “ Drawing ”, Structural variables are compared to “ Spare parts ”, The characteristics of parts produced according to the same drawing are the same ;
- A struct is a data type , Is a template for creating variables , It doesn't take up memory space ;
- Structural variables contain real data 、 Need storage space ;
3、 ... and 、 Structure member access
- Structure variables use . visit ;
- Structural variable . object
#include<stdio.h>
#include<string.h>
struct Date
{
int year;
int month;
int day;
};
struct Student
{
char s_name[20];
struct Date birthday;
float score;
};
int main()
{
struct Student stu = { "liuwen",2000,10,1,99.9 };
printf("name=%s\nbirtyday=%d.%d.%d\nscore=%f\n", stu.s_name, stu.birthday.year, stu.birthday.month, stu.birthday.day, stu.score);
stu.score = 77;
printf("name=%s\nbirtyday=%d.%d.%d\nscore=%f\n", stu.s_name, stu.birthday.year, stu.birthday.month, stu.birthday.day, stu.score);
return 0;Be careful : There are three cases of overall assignment of structural variables
- Define structure variables ( use {} initialization )
- Initialize with a defined structure variable
- Variables with the same structure type can be assigned to each other as a whole ;
stay C There is no casting of struct type in language .
Four 、 Structure variables and pointers
Built in types can define pointer variables , Structure types can also define structure type pointers ;
The structure type pointer accesses the acquisition and assignment form of the member :
(1)(*p). Member name (. Has a higher priority than *,(*p) The brackets on both sides cannot be less )
(2) p-> Member name (-> Pointer )
#include<stdio.h>
#include<string.h>
struct Inventory// goods
{
char description[20];// Name of goods
int quantity;// Inventory data
};
int main()
{
struct Inventory sta = { "iphone",20 };
struct Inventory* stp = &sta;
char name[20] = { 0 };
int num = 0;
(*stp).quantity = 30;
stp->quantity = 30;
strcpy_s(name,sizeof(stp->description),stp->description);
printf("%s %d\n", stp->description, stp->quantity);
printf("%s %d\n", (*stp).description, (*stp).quantity);
return 0;
}Structure and function
#include<stdio.h>
#include<string.h>
#define _CRT_SECURE_NO_WARNINGS
struct School
{
char s_name[20];// School
int s_age;
};
void Print_a(struct School sx)
{
printf("%s %d\n", sx.s_name, sx.s_age);
}
void Print_c(struct School* sp)
{
printf("%s %d\n", sp->s_name, sp->s_age);
}
int main()
{
struct School sx = { "xi'an",100 };
Print_a(sx);
Print_c(&sx);
return 0;
}5、 ... and 、struct and class The difference between
1、 What is? class?
class( class ) Is the basic concept of object-oriented programming , Is a custom data structure type , Usually contains fields 、 attribute 、 Method 、 attribute 、 Constructors 、 Indexer 、 Operators, etc. . Because it's a basic concept , So there is no need to describe in detail here , Readers can inquire relevant concepts to understand . What we emphasize is .NET in , All classes eventually inherit from System.Object class , Therefore, it is a reference type , in other words ,new An instance of a class , Object stores the reference address of the actual data of the instance , The value of the object is stored in the managed heap (managed heap) in .
2、 What is? struct?
struct( structure ) Is a value type , It is used to organize a group of related information variables into a single variable entity . All structures are inherited from System.ValueType class , So it's a value type , in other words ,struct The instance is allocated on the stack of the thread (stack) On , It stores the value itself , Without a pointer to the value . So it's using struct when , We can think of it as int、char Such basic type classes treat .
- 1. Default inherited permissions ,class According to private Inheritance processing ,struct According to public Inheritance processing ;
- 2. Default access rights for members .class The default member of is private jurisdiction ,struct The default is public jurisdiction .
- 3.struct Value type ,class Is object type
6、 ... and 、 Reference resources
边栏推荐
- Deep understanding of JVM (II) - memory structure (II)
- Communication network electronic billing system based on SSH
- Mo Tianlun salon | Tsinghua qiaojialin: Apache iotdb, originated from Tsinghua, is building an open source ecological road
- The secondary menu of the magic article system v5.4.0 supports the optimization of form display
- Apache parsing vulnerability (cve-2017-15715)_ Vulnerability recurrence
- 基于eNSP的校园网设计的仿真模拟
- [BJDCTF2020]The mystery of ip|[CISCN2019 华东南赛区]Web11|SSTI注入
- Small Tools(3) 集成Knife4j3.0.3接口文档
- NFT: 开启加密艺术时代的无限可能
- [PROJECT] Xiaomao school (IX)
猜你喜欢

News management system based on SSM

Simulation of campus network design based on ENSP

TCP session hijacking based on hunt1.5

Communication network electronic billing system based on SSH
![[PROJECT] Xiaomao school (IX)](/img/01/f7fc609e7a156d6e60ce6482ba2ac1.jpg)
[PROJECT] Xiaomao school (IX)

Mo Tianlun salon | Tsinghua qiaojialin: Apache iotdb, originated from Tsinghua, is building an open source ecological road

构建基本buildroot文件系统

Spin lock exploration

基於SSH的網上商城設計

每日面试1题-如何防止CDN防护被绕过
随机推荐
Small tools (3) integration knife4j3.0.3 interface document
Daily interview 1 question - basic interview question of blue team - emergency response (1) basic idea process of emergency response +windows intrusion screening idea
同济、阿里的CVPR 2022最佳学生论文奖研究了什么?这是一作的解读
Redis (IX) - enterprise level solution (II)
ASP. Net generate verification code
阿里云ECS导入本地,解决部署的问题
[software testing] basic knowledge of software testing you need to know
后渗透之文件系统+上传下载文件
每日面试1题-如何防止CDN防护被绕过
The company was jailed for nonstandard bug during the test ~ [cartoon version]
C语言结构体
Word中添加代码块(转载)
[Architecture] 1366- how to draw an excellent architecture diagram
What will be the game changes brought about by the meta universe?
Switching routing (VLAN) experiment
港科大&MSRA新研究:关于图像到图像转换,Finetuning is all you need
漏洞复现----37、Apache Unomi 远程代码执行漏洞 (CVE-2020-13942)
Redis (II) -- persistence
Post office - post office issues (dynamic planning)
Inventory in the first half of 2022: summary of major updates and technical points of 20+ mainstream databases