C language: Essay 10-Union

Union concept: The structure that uses several different variables to occupy a section of memory is called "union" type structure.

The general form of defining a union type variable is:

union 共用体名
{
   成员列表
}变量列表; 
//有点类似于结构
//例子
union data
{
   int i;
   char ch;
   float f;
}a,b,c;//abc表示这个共用体的变量
//或者可写为:
union data
{
   int i;//占4个子节
   char ch;//占1个子节
   float f;//占4个字节
};
union data a,b,c;//所以在整个共用体内共用体共占4个字节,不是所有之和9。

Comparison of union and structure:

(1) The memory length occupied by structure variables is the sum of the memory length occupied by each member. Each member occupies its own memory unit.

The memory length occupied by the union variable is equal to the length of the longest member. (For example, the "union" variables a, b, and c defined above each occupies 4 bytes (because a real and integer variable both occupy 4 subsections) instead of 4+1+4=9 subsections each )

(2) The union variable can only be referenced if it is defined first, and the union variable cannot be referenced, but only the members in the union variable.

For example, previously defined a, b, and c as union variables. ai (refer to the integer variable i in the union variable) a.ch,af. But the variable a cannot be used for reference.

(3) The same memory segment can be used to store different types of members in the machine, but only one of them can be stored at a moment, rather than several at the same time. (It is not compatible) (Only one can be stored at a time)

(4) The active member in the union variable is the last member stored, and the original member loses its effect after storing a new member. (Because the last stored has been overwritten by the next)

(5) The address of the union variable and the addresses of its members are the same address.

(6) It is not possible to assign a value to a union variable name, nor can it attempt to quote a variable name to obtain a value, nor can it be initialized when the union variable is defined.

(7) The union variable cannot be used as a function parameter. Boone also makes the function wait for the union variable, but you can use a pointer to the union variable.

(8) The union type can appear in the structure type definition, or it can define the union array. Conversely, the structure can also appear in the union type definition, and the array can also be a member of the union.

Example: Suppose there are data for several persons, among them are students and teachers. The student's data includes: name, number, gender, occupation, class. Teacher's data includes: name, number, gender, occupation, plant. It can be seen that the data contained in students and teachers are different. It is now required to put them in the same form.

Code:

#include<stdio.h>
struct//定义一个结构,因为他还有其他的一些数据
{
  int num;
  char name[10];
  char sex;
  char job;
  union
  {
     int banji;
     char position[10];
  }category;//是班级或者是职务,那么我们定义一个union,任选其一

}person[2];//为了方便假设一个学生一个老师

void main()
{
   int I;
   for(I=0;i<2;i++)
   {

   }

}

result:

---Enumerated type:

In practical problems, the values ​​of some variables are limited to a limited range. (For example, there are only 7 days in a week.) It is obviously inappropriate to specify these quantities as integers, character types or other types. Therefore C language provides an "enumeration" type.

If the variables a, b, and c are described as the weekday of the mountain rat, any of the following methods can be used:

//第一种
enum weekday{sum,mou,tue,wed,thu,fir,sat};
enum weekday a,b,c;
//第二种
enum weekday{sum,mou,tue,wed,thu,fir,sat}a,b,c;
//第三种
enum {sum,mou,tue,wed,thu,fir,sat};

----typedef defines the type

typedef is an alias for a known type name.

typedef declares a new type name to replace the existing type name.

//比如声明INTEGER为整型
typedef int INTEGER;//为int这个整型取一个别名叫做INTEGER,只要在程序中出现了INTEGER就是int。

//再加深,比如说我们要声明的是结构类型
#include <stdio.h>
Typedef struct
{
    int month;
    int day;
    int year;
}DATE;//为struct这个结构取一个别名叫做DATE。
//事实上DATE就是上边的结构。

void main()
{
    DATE date_one;//用一个结构定义一个变量
    date_one.month=9;
    date_one.day=22;
    date_one.year=2020;//在为date_one赋值。
    printf("%d-%d-%d-\n",date_one.month,date_one.day,date_one.year);
}

Use typedef to declare an integer array type for NUM:

//Declaration format:
typedef int NUM[100];//Explain that his type is int integer, and he has 100 variables

#incluede<stdio.h>
typedef int NUM[100];//说明他的类型是int整型的,他有100个变量
void main()
{
    NUM num={0};//用NUM来定义一个小的num,并给他赋初值//此处注意了NUM不用加后边的[100],因为你只是一个修饰,修饰说上边的这个名称NUM数组有多少个成员变量而已。所以这里只要用他的名字就行了
    printf("%d\n\n",sizeof(num));
}

Declare STRING as a character pointer type:

typedef char* STRING

#include<stdio.h>
typedef char* P;//为char*起别名叫做P
void main()
{
   P p1;//用P来命名p1
   p1="I lOVE";//p1指向一个字符串常量
   printf("%s",p1);//打印
}

Declare POINTER as a pointer to a function and return an integer value.

typedef int (*POINTER)();//Following a bracket () indicates that it is a function, and another bracket (*POINTER) indicates that the function name pointed to is pointed to by a pointer.

#include<stdio.h>

typedef void (*P)();
void fun();
void main()
{
    P p1;//用P声明了一个p1指针,这个p1指针是(*P)()这个类型//实际上就是这样一个声明 void (*p1)();
    p1=fun;//p1指向fun//fun是函数的名称,(函数名称和这个函数的地址就是同一个东西,所以此处加不加“&”符(即&fun)都是是一样的)刚好可以把他替代进去
    (p1)();//上边一句只是赋值,还没有调用。//它是一个函数,所以要加上括号

}
void fun()
{
    printf("I Love");
}

Note: Typedef can declare various type names, but cannot be used to define variables.

Typedef just adds a type name to an existing type, but does not call a new type.

When the same type of data is used in different source files, typedefs are often used to declare some data types, put them in a single file, and then use the #include command to include them in the files that need them.

Typedef is similar to #define, for example: typedef int COUNT; #define COUNT int is used to represent int with COUNT. However, the two are different.

#define is processed in pre-compilation, it is just a simple string replacement, and typedef is processed at compile time. In fact, it is not a simple string replacement, but a revolutionary type using the same method as defining variables.

typedef (int*) p1;//The name p1 refers to an integer pointer.

Guess you like

Origin blog.csdn.net/m0_37957160/article/details/108735367