当前位置:网站首页>Finish C language
Finish C language
2022-06-11 09:47:00 【51CTO】
Table of Contents
The declaration of the consortium is similar to the structure , But his behavior is different from the structure . All members of the consortium refer to Same location in memory . When you want to store different things in the same place at different times, you can use a consortium .
Take a simple example .
In a floating-point and integer are 32 On the machine , Variable fi Occupy only one memory 32 Bit words , When f When used , This word is accessed as a floating-point value ; single i When used , This word is accessed as an integer value . Simply put, the same memory , Translated into different types of data , How to translate depends on the data type defined .
Running result of the above program .
The role of the consortium
Why use a consortium , When you just want to see how floating point numbers are stored on a particular machine , But not interested in other types , The consortium may be right for you . Take an example .
BASIC You need to remember the variable values used by the program , He provides several different types of variables . The type of each variable must be stored with its value . Here a structure is defined to store this information , But it's not efficient .
When BASIC When a variable is created in a program , Just create a structure of this type to record the types of variables , According to the type of variable , Store the value of the variable in one of the three fields . The inefficiency of this structure lies in , Two fields are wasted each time , Because a variable has only one data type .
Union can reduce this waste , Storing three fields in the same memory area does not cause conflicts , At some point , Only one field in the Union will be used .
If each member of the consortium has different variables , The length of the union is equal to Length of the longest member .
Consortium - Variation record
A specific area of memory will store different types of values at different times , In some cases , These values are more complex than simple integer or floating point types , Each of them is a complete structure .
This example is taken from an inventory system , He records two different entities : Spare parts (part) And assembly parts (subassembly).
Next inventory (inventory) General information including each item is recorded , Include a union , Or for storing part information , Or it can be used to store the information of spare parts .
In a consortium with different member lengths , The amount of memory allocated to the Federation depends on the maximum member length . If these members differ greatly in length , Memory is wasted when storing short members , In this case, a better way is to store the combined weight Pointers to different members , Not the storage member itself . All pointers have the same length , This solves the problem of memory waste . When you decide to store that member , Allocate the correct amount of memory to store the member .
Initialization of the union
Joint variables can be initialized , But this initial value must be joint The type of the first member , And it must be in a pair of curly braces . for example ,
x.a Initialize to 5.
We cannot initialize this variable to a floating-point or character value . If the given type is any other type of value , Will be forcibly converted to an integer and assigned to x.a.
One 、 Basic concepts
enumeration enum It's English enumerate Abbreviation , That is, enumerate 、 The meaning of the arrangement description . The enumeration type is C A special type of language , Enumeration types allow our program to use some variable value ranges with fixed length and fixed value . Define enumeration types :enum Enumeration type { Enumerate the list of values };
Two 、 Use of enumeration types
1、 Define enumeration types
All the variables enumerated in this way are push variables , When referencing :
That is, direct use of , The corresponding value will be output directly .
2、 Define variables of enumeration type
3、 Call enumeration type variables
The same goes for the weekly date and the monthly output :
4、 If you specify the value of an element when defining an enumeration type , You can also change the value of the enumeration element
for example :
enum weekday{sun=7,mon=1,tue,wed,thu,fri,sat}day;
At this time ,sun by 7,mon by 1, After that, the elements are added in sequence 1, therefore sat Namely 6 了 .
5、 Enumeration values can be used for judgment
for example :
if (day==month) {…}
if (day>month) {…}
The comparison rule for enumeration values is : Compare according to the sequence number in the description , If there is no man-made specification , Then the value of the first enumeration element is recognized as 0.
6、 An integer cannot be assigned directly to an enumeration variable , You must force a type cast to assign a value
3、 ... and 、 Enumerate cases
Input 0-6 A number in , The corresponding week can be output .
边栏推荐
- 整型提升例题
- Concurrent programming
- 【clickhouse专栏】新建库角色用户初始化
- JS foundation -- about DOM
- The ins-30131 installer failed to verify the required initial settings
- Slice of go language foundation
- Detailed explanation of this and static
- The first TOF related data set available for deep learning: deep learning for confidence information in stereo and TOF data fusion (iccv 2017)
- 关于CI框架批量导出至压缩文件
- [image denoising] image denoising based on median + wavelet + Wiener + filter, including Matlab source code
猜你喜欢

DOS command virtual environment

Bowen dry goods | Apache inlong uses Apache pulsar to create data warehousing

ESP8266_GET请求天气预报、json解析

PD chip ga670-10 for OTG while charging

ESP8266_SNTP(Simple Network Time Protocol)

VK2C22A/B抗干扰系列电表,水表段码LCD液晶驱动芯片资料(可定制DICE/COG)

Before applying data warehouse ODBC, you need to understand these problems first

Type-C Bluetooth speaker single port rechargeable OTG solution

ESP8266_ Access to the core suite of Baidu Internet of things and use mqtt protocol for communication

affair
随机推荐
Thread theory
Flask (IV) -- URL construction
Oracle XDB组件的重建
C+每日练题(15)
优秀测试工程师必备的4项安全测试方法!
Day44 database
Where is it safer to open an account for soda ash futures? How much does it cost to buy at least one hand?
Image quality evaluation including Matlab source code
Use of MSF evaluation module
Revisiting Self-Training for Few-Shot Learning of Language Model,EMNLP2021
【ROS】noedic-moveit安装与UR5模型导入
Control statement if switch for while while break continue
Oracle DG physical standby database uses alias data file to change path to OMF path
The difference and relation between machine learning and statistics
The first TOF related data set available for deep learning: deep learning for confidence information in stereo and TOF data fusion (iccv 2017)
js基础--关于DOM
Opencv image basic operation (IV) -- image feature extraction (corner detection)
Identifier keyword literal data type base conversion character encoding variable data type explanation operator
How do we connect to WiFi?
完结C语言