C language: structure, union


1. Structure

A structure is a collection of elements of different element types

1. Declaration of structure and definition of structure variable

The declaration of the structure consists of three parts, the tag name (tag), the member list (member-list), and the variable list (variable-list).
If we want to declare a student type, as follows:
insert image description here
If we want to define variables, we have two ways.

  • Defining variables directly with struct types
  • Define variables in the variable list

as follows:

//在变量列表中定义变量s1,s2
struct Student
{
    
    
	char name[20];//名字
	int age;//年龄
	char sex[5];//性别
	char id[20];//学号
}s1; s2;


int main()
{
    
    
	//使用结构体类型定义变量s3
	struct Student s3;

	return 0;
}

In addition, we can also make anonymous declarations (incomplete declarations), as follows:

//不完全声明
struct
{
    
    
	char c;
	short b;
	int a;
};

In this case, we want to define variables, only in the variable list, because we don't know the type of the structure.
as follows:

//定义变量s1,s2,s3
struct
{
    
    
	char c;
	short b;
	int a;
}s1; s2; s3;

If we don't declare two identical structs exactly, the compiler will think the two structs are of different types.
as follows:

//定义变量s1,s2,s3
struct
{
    
    
	char c;
	short b;
	int a;
}s1; s2; s3;

//定义指针ps
struct
{
    
    
	char c;
	short b;
	int a;
}*ps;

int main()
{
    
    
	ps = &s1;

	return 0;
}

insert image description here

2. Structure variable initialization

There are two ways to initialize structure variables.

  1. Variables can be initialized in the variable list
struct Student
{
    
    
	char name[20];//名字
	int age;//年龄
	char sex[5];//性别
	char id[20];//学号
}s1 = {
    
     "李四", 20, "男", "123456" };
  1. When defined with a type, initialize the variable
struct Student
{
    
    
	char name[20];//名字
	int age;//年龄
	char sex[5];//性别
	char id[20];//学号
};

int main()
{
    
    
	struct Student s1 = {
    
     "李四", 20, "男", 123456 };

	return 0;
}

3. Access structure members

For structure variables to access its members, we use the . operator to access.
For the structure pointer variable to access the members it points to, we use the -> operation method to access.
as follows:

struct A
{
    
    
	char a;
	int b;
};

int main()
{
    
    
	struct A a = {
    
     0 };
	struct A* pa = &a;

	a.b = 10;
	printf("%d\n", a.b);

	pa->b = 20;
	printf("%d\n", pa->b);

	return 0;
}

The result is as follows:
insert image description here

4. Memory alignment of structures

To introduce the memory alignment of the structure, we have to calculate the size of the structure.
What is the size of the following structure?

struct A
{
    
    
	char a;
	int b;
	char c;
};

int main()
{
    
    
	printf("%d\n", sizeof(struct A));
	return 0;
}

The answer is 12, this is because the structure is memory aligned.
To know how 12 is calculated, we need to master the relevant rules about aligned numbers.

  • The first struct member is always at offset 0.
  • Other member variables should be aligned to an address that is an integer multiple of its alignment number
  • The total size of the structure is an integer multiple of the maximum alignment (the alignment of its member variables)
  • If a structure is nested, the nested structure is aligned to an integer multiple of the maximum alignment number of bytes, and the overall size of the structure is an integer multiple of the maximum alignment number (the nested structure is also a member of the structure)

Alignment = The lesser of the compiler's default alignment and the size of the member. If the compiler does not have a default alignment, then the alignment is the size of the member itself.

insert image description here
So far, we know how to get 12. But why should there be memory alignment?

  1. Platform reasons (transplantation reasons)
    Not all hardware platforms can access any data at any address; some hardware platforms can only fetch certain types of data at certain addresses.

  2. Performance Reasons
    Data structures (especially stacks) should be aligned on natural boundaries as much as possible.
    To access an unaligned memory size, the processor needs to do two memory accesses; an aligned memory access requires only one access.

2. Union (Community)

As the name implies, a union means that members of this type use the same memory space.
As follows:
declare the union type union A, whose members are char a, int b.

union A
{
    
    
	char a;
	int b;
};

So what is its size?

union A
{
    
    
	char a;
	int b;
};

int main()
{
    
    
	printf("%d\n", sizeof(union A));

	return 0;
}

The answer is 4, why?

  1. The size of the union is at least the size of its largest member
  2. The size of the union must also be an integer multiple of its alignment

insert image description here
As shown in the figure above, its members a and b share the first byte. If we use member b together when using member a, then how much is a =?

union A
{
    
    
	char a;
	int b;
};


int main()
{
    
    
	union A un = {
    
     0 };
	un.a = 1;
	un.b = 10;

	printf("%d\n", un.a);

	return 0;
}

The answer is a = 10. We only need to look at the changes in memory.
insert image description here


Summarize

insert image description here
The above is my summary of knowledge about structures and unions.

Guess you like

Origin blog.csdn.net/li209779/article/details/131889487