Points to note when using structures, unions, and enumeration variables

1. Structure

The structure stores different types of data together and processes them as a whole, which is very convenient to use. The following is an example of structure declaration and creation

//申明一个结构体类型 
struct food
{
    
    
    char name[64];				//名称
    float price;				//价格
};

struct food	ChineseFood;

1.1 Precautions

The position of the struct declaration determines the scope of the structure. If it is placed outside any function, then all functions after the declaration in this file can be used; if it is declared inside a function, it can only be used inside the function and After the declaration use

After creating a structure variable, the compiler will allocate memory space for the variable, and the storage space is contiguous

1.2 Initialization

//方法1,只存在于c99,不需要严格按照变量顺序
struct food food1 =
{
    
    
	.name = "aaa",
    .price = 10
};

//方法2
struct food food2 =
{
    
    
	"bbb",
    20
};

1.3 Structure array

Each element of the array is a structure, structure array format: declare variable type array name [array length]

1.4 Struct self-reference

Struct self-referencing can be done in the following ways:

//方法1
struct st
{
    
    
    struct st *a; 	//因为指针的长度是确定的
    int b;
};

//方法2
typedef struct str1
{
    
     
	int value; 
	struct str1 *link; 
} st;

1.5 Structs nested in other structs

struct student
{
    
    
	char name[32];
	int age;
	int score;
};
struct teacher
{
    
    
	char name[32];
	int age;
	student s1;
};

1.6 Remarks

In the use of the structure, it is illegal. Generally, when the structure is used in nested use, the structure name has not been defined, so the size of the declared structure cannot be determined - it can be avoided by using pointers

2. Consortium

A union is a special custom type that contains a series of members, but these member variables share the same space, also called a union
declaration similar to a structure

union un
{
    
    
	char c;
	int i;
};
  1. Comply with the principle of structure alignment, the total number of bytes of the union should be the minimum integer multiple of the maximum alignment number among the elements of the union
#include<stdio.h>

union MemMode
{
    
    
	int a;
	char b;
};


int main()
{
    
    
	union MemMode memMode;
	memMode.a = 0x11223344;
	printf("%x\n", memMode.a);		//a = 0x11223344
	memMode.b = 0x55;
	printf("%x\n", memMode.a);		//a = 0x11223355 或者 0x55223344(取决于存储模式),联合体最大4字节,a的值被后面共用位置的b的赋值修改
  1. Using low address to store elements, the compiler storage method can be obtained through the union
#include<stdio.h>

union MemMode
{
    
    
	int a;
	char b;
};


int main()
{
    
    
	union MemMode memMode;
	memMode.a = 0x11223344;
	if (memMode.b == 0x11)
	{
    
    
		printf("大端存储\n");
	}
	else if (memMode.b == 0x44)
	{
    
    
		printf("小端存储\n");
	}
}

Three, enumeration

statement:enum 枚举名 { 元素1, 元素2 };

  1. The default value of the first enumeration member is an integer of 0, and the value of subsequent enumeration members is added to the previous member by 1
  2. Enumerations are value types that store data directly, not references, so they cannot be modified
  3. Enumeration has only one type of member: int integer constant

4. Calculation of length bytes

Guess you like

Origin blog.csdn.net/future_sky_word/article/details/125610678