当前位置:网站首页>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 .
边栏推荐
- Control statement if switch for while while break continue
- 《1.图书馆里系统》
- jedisLock—redis分布式锁实现
- 2022 must have Chrome extension - browser plug-in to double your Internet efficiency
- Oracle DG physical standby database uses alias data file to change path to OMF path
- ESP8266_ SNTP(Simple Network Time Protocol)
- js中关键字this的理解
- js中的事件
- Flask (IV) -- URL construction
- [ROS] noedic moveit installation and UR5 model import
猜你喜欢

DOS command virtual environment

考研數學 【數列極限證明題】題型方法總結

Reconstruction des composants Oracle XDB

考研数学 【数列极限证明题】题型方法总结

An error can't locate env pm in @INC
![[TiO websocket] v. TiO websocket server counts the number of online people](/img/05/77f890a0f535ff955dffe8ca404442.jpg)
[TiO websocket] v. TiO websocket server counts the number of online people

ESP8266_SmartConfig

ORACLE DG物理备库使用别名数据文件改变路径到OMF路径

Ora-00059 exceeds DB_ Files limit

Exclusive interview - dialogue on open source Zhai Jia: excellent open source projects should be seen by more people. I am honored to participate in them
随机推荐
oracle 11g rac 磁盘组有空间无法增加数据文件?
Opencv image basic operation (III) -- image feature extraction (corner detection)
考研數學 【數列極限證明題】題型方法總結
C+每日练题(15)
Thread theory
Flask (IV) -- URL construction
2022 must have Chrome extension - browser plug-in to double your Internet efficiency
等待事件 enq: KO - fast object checkpoint可行的一些处理方法
Tap and longtap encapsulation of touch events -- from Ono
Where is it safer to open an account for soda ash futures? How much can soda ash futures do now?
Four data-driven behaviors of integral system
CVE-2021-40449 NtGdiResetDC UAF
The first TOF related data set available for deep learning: deep learning for confidence information in stereo and TOF data fusion (iccv 2017)
Sword finger offer II 041 Average value of sliding window
MySQL:Got a packet bigger than ‘max_ allowed_ packet‘ bytes
Flask (III) -- variable rules
我们是如何连上WiFi的?
Use of MSF evaluation module
Leetcode brushing questions - hand tearing binary tree
Touch事件的tap,longtap封装--来自小野