当前位置:网站首页>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 .
边栏推荐
- ESP8266_ SNTP(Simple Network Time Protocol)
- CVPR 2021: learning continuous image representation with local implicit image function
- Troubleshooting the error ora-12545 reported by scanip in Oracle RAC
- Rebuilding Oracle XdB components
- Monotone stack
- DOS command virtual environment
- document对象
- ORACLE RAC中连接ScanIP报错ORA-12545的问题解决
- Tenthousand words thoroughly learn heap and binary tree
- 2022 must have Chrome extension - browser plug-in to double your Internet efficiency
猜你喜欢

PD chip ga670-10 for OTG while charging

Concurrent programming

Day41 process pool and thread pool

The first TOF related data set available for deep learning: deep learning for confidence information in stereo and TOF data fusion (iccv 2017)

ESP8266_ SNTP(Simple Network Time Protocol)

How do online app stores of laundry chain stores do?

Revisiting Self-Training for Few-Shot Learning of Language Model,EMNLP2021

Video review pulsar summit Asia 2021, cases, operation and maintenance, ecological dry goods

Leetcode brushing questions - hand tearing binary tree

Day45 storage engine data type integer floating point character type date type enumeration and set type constraints table to table relationships
随机推荐
「INS-30131」 安装程序验证所需的初始设置失败
Don't use redis list to implement message queue. Stream is designed for queues
MSF adds back door to normal program
P1169 "chessboard making"
Flask (IV) -- URL construction
【音视频】SEI简介
Touch事件的tap,longtap封装--来自小野
OpenSSL usage
等待事件 enq: KO - fast object checkpoint可行的一些处理方法
Events in JS
2022 must have Chrome extension - browser plug-in to double your Internet efficiency
js基础--Array对象
RSA签名问题
CVPR 2021: learning continuous image representation with local implicit image function
优秀测试工程师必备的4项安全测试方法!
Tap and longtap encapsulation of touch events -- from Ono
Suffix Array
《1.图书馆里系统》
RAC expdp export error: ora-31693, ora-31617, ora-19505
Ecological co construction | 2021 streamnational excellent partner of the year comes out!