当前位置:网站首页>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
边栏推荐
- Tensorflow2 深度学习十必知
- Thinking on large file processing (upload, download)
- Deep understanding of JVM (V) - garbage collection (II)
- Design of online shopping mall based on SSH
- Radio and television 5g officially set sail, attracting attention on how to apply the golden band
- It's not easy to say I love you | use the minimum web API to upload files
- Redis (VIII) - enterprise level solution (I)
- Post MSF infiltration summary
- TCP session hijacking based on hunt1.5
- Customer relationship CRM management system based on SSH
猜你喜欢

It's not easy to say I love you | use the minimum web API to upload files

Switching routing (VLAN) experiment

What did Tongji and Ali study in the CVPR 2022 best student thesis award? This is an interpretation of yizuo

Redis (V) - advanced data types

构建基本buildroot文件系统

Redis (VII) - sentry

漏洞复现----38、ThinkPHP5 5.0.23 远程代码执行漏洞

News management system based on SSM

Importing alicloud ECS locally to solve deployment problems

大文件处理(上传,下载)思考
随机推荐
MySQL reports that the column timestamp field cannot be null
One script of unity actual combat realizes radar chart
每日面试1题-如何防止CDN防护被绕过
NFT铸造交易平台开发详情
Communication network electronic billing system based on SSH
【剑指Offer】剑指 Offer 53 - II. 0~n-1中缺失的数字
又一篇CVPR 2022论文被指抄袭,平安保险研究者控诉IBM苏黎世团队
Grep output with multiple colors- Grep output with multiple Colors?
Customer relationship CRM management system based on SSH
港科大&MSRA新研究:关于图像到图像转换,Finetuning is all you need
vue3 响应式数据库—— reactive
[bjdctf2020]the mystery of ip|[ciscn2019 southeast China division]web11|ssti injection
后渗透之文件系统+上传下载文件
What does software testing need to learn? Test learning outline sorting
Small tools (3) integration knife4j3.0.3 interface document
Deep understanding of JVM (I) - memory structure (I)
The company was jailed for nonstandard bug during the test ~ [cartoon version]
大文件处理(上传,下载)思考
Flutter custom component
【二叉树】前序遍历构造二叉搜索树