当前位置:网站首页>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 .

union {
	float f;
	int i;
}fi;

fi.f = 3.14159;
printf("fi.i = %d\n", fi.i);

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

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 .

fi.i =  1078530000

     
  • 1.

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 .

struct VARIABLE{
	enum {INT, FLOAT,STRING} type;
	int   int_value;
	float  float_value;
	char    *string_value;
};

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

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 .

struct VARIABLE {
	enum { INT, FLOAT, STRING} type;
	union {
		int    i;
		float  f;
		char   *s;
	}value;
};

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

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).

struct PARTINFO {
	int  cost;
	int   supplier;
	...
};
struct SUBASSYINFO {
	int  n_parts;
	struct {
		char  partno[10];
		short  quan;
	}parts[MAXPARTS];
};

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

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 .

struct INVREC {
	char partno[10];
	int quan;
	enum {PART, SUBASSY} type;
	union {
		struct PARTINFO part;
		struct SUBASSYINFO  subassy;
	}info;
};

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

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 ,

union {
	int   a;
	float  b;
	char   c[4];
}x = {5};

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

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

// Enumerate colors 
enum color{red=1, oreange=2, yellow=3, green=4, ching=5, blue=6, purple=7};

// Enumerate every day of the week 
enum week
{
	Su, Mo, Tu, We, Th, Fr, Sa
};

// Enumerate every month 
enum month
{
	January, February, March, April, May, June, July, August, September, October, November, December
};

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

All the variables enumerated in this way are push variables , When referencing :

printf("%-3d %-3d %-3d %-3d %-3d %-3d %-3d", red, oreange, yellow, green, ching, blue, purple); 

     
  • 1.

That is, direct use of , The corresponding value will be output directly .

2、 Define variables of enumeration type

enum week a, b, c;
a = Su;
b = Mo;
c = Tu;
 
enum month d, e, f;
d = January;
e = February;
f = March;

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

3、 Call enumeration type variables
The same goes for the weekly date and the monthly output :

enum week a, b, c;
a = Su;
b = Mo;
c = Tu;
printf("%d %d %d\n", a, b, c);
enum month d, e, f;
d = January;
e = February;
f = March;
printf("%d %d %d\n", d, e, f);

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

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

#include <stdio.h>
void main() 
{ 
	enum weekday {sun,mon,tue,wed,thu,fri,sat} day; 
	int k; 
	printf(" Please enter 0 To 6 Number of numbers :"); 
	scanf("%d",&k);
	day=(enum weekday)k; 

	switch(day) 
	{ 
		case sun: printf("sunday\n");break; 
		case mon: printf("monday\n");break; 
		case tue: printf("tuesday\n");break; 
		case wed: printf("wednesday\n");break; 
		case thu: printf("thursday\n");break; 
		case fri: printf("friday\n");break; 
		case sat: printf("satday\n");break; 
		default: printf("input error\n");break; 
	} 
} 

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

Input 0-6 A number in , The corresponding week can be output .

原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110932586221.html