[C language] custom enumeration type structure union

Knowledge base structure
structure is a collection of values, these values are known as member variables. Each member structure may be different types of variables.
Declaration of structure of
the definition of a student's structure

struct Stu
{
 char name[20];//名字
 int age;//年龄
 char sex[5];//性别
 char id[20];//学号
}//分号不能丢

struct Stu s = {"zhangsan", 20"man",110};//初始化

A structure derived from the application

struct Node
{
 int data;
 struct Node* next;//指向自身类型的指针
};
//链表类型

Alignment structure of the memory
is the size of the structure is calculated.
Alignment rules must first master the structure:

  1. A first member and a structure variable offset amount is 0 at the address.
  2. Other member variables to align the start address to the address of an integer multiple digital (alignment number).
    Alignments = compiler small value a default size and the number of aligned members.
    The default value VS 8
    default value 4 in Linux
  3. The total size of the structure is a maximum alignment (alignment of each member has a variable number) is an integer multiple.
  4. If the case where the nested structure, the nested structure is aligned to an integer multiple of the maximum number of their alignment, the overall size of the structure is that all maximum alignment (alignment number including nested structure) is an integer multiple.
struct s1
{
	char c1;
	int i;
	char c2;
};//12
struct s2
{
	char c1;
	char c2;
	int i;
};//8
struct s3
{
	int i;
	char c1;
	char c2;
};//8
struct s4
{
	int i;
	char c1;
	double d;//8
};//16
struct s5
{
	int i;
	double d;//8
	char c1;
};//24
struct s6//结构体嵌套问题
{
	int i;
	char c1;
	struct s4 s;//总大小16
};//24
struct s7
{
	int i;
	struct s4 s;//16
	char c1;
};//32

Structure parameter passing

struct S
{
 int data[1000];
 int num;
};
struct S s = {{1,2,3,4}, 1000};
//结构体传参
void print1(struct S s)
{
 printf("%d\n", s.num);
}
//结构体地址传参
void print2(struct S* ps)
printf("%d\n", ps->num);
}
int main()
{
 print1(s);  //传结构体
 print2(&s); //传地址
 return 0;
}

Two kinds of parameter passing mode analysis:
function parameter passing time, is the need to push the parameters, there will be overhead in time and space.
If you pass a structure of an object when the structure is too large, the pushing of the parameters of the system overhead is relatively large, it will lead to performance degradation. Conclusion: The structure parameter passing time, and shall address structure.

Enumeration
Enumeration name suggests is to enumerate.
For example, there are seven days a week

enum Day//星期
{
 Mon,
 Tues,
 Wed,
 Thur,
 Fri,
 Sat,
 Sun
};

{} The contents of the possible values are enumerated type, also called enumeration constants.
The possible values are all values from zero by default, incremented by one once, of course, it can also be defined at the time of initial value.
Enumerated advantages:
we can use a #define constants, why have to use enumeration? Enumeration of advantages:

  1. Increase readability and maintainability of the code
  2. And #define defined identifiers have relatively enumeration type checking, more rigorous.
  3. Prevents contamination name (package)
  4. Easy to debug
  5. Easy to use, once you can define multiple constants

Enumeration of use

enum Color//颜色
{
 RED=1,
 GREEN=2,
 BLUE=4
};
enum Color clr = GREEN;//只能拿枚举常量给枚举变量赋值,才不会出现类型的差异。

Joint (union)
combined type definition: joint is a special type of custom-defined variables of this type also comprise a number of members, wherein the members with a common space (it is also called union joint). such as:

//联合类型的声明
union Un
{
 char c;
 int i;
};
//联合变量的定义
union Un un;
//计算连个变量的大小
printf("%d\n", sizeof(un)); 

The combined characteristics of
union members are sharing the same memory space, so the size of a joint variable, at least the size of the largest member of (at least since the joint must have the ability to save the largest members).

union Un
{
 int i;
 char c;
};
union Un un;
// 下面输出的结果是一样的 因为共用一块内存
printf("%d\n", &(un.i));
printf("%d\n", &(un.c));

un.i = 0x11223344;
un.c = 0x55;
printf("%x\n", un.i);//输出0x1223355

Calculation of joint size
Joint size is at least the size of the largest member.
When the maximum number is not an integer multiple of the alignment of the largest members of the size, it is necessary to align the maximum number of integer multiple of alignment.

union Un1
{
 char c[5];
 int i;
};
union Un2
{
 short c[7];
 int i;
};

printf("%d\n", sizeof(union Un1));//8
printf("%d\n", sizeof(union Un2)); //16
Published 29 original articles · won praise 8 · views 2003

Guess you like

Origin blog.csdn.net/qq_44785014/article/details/103214191