115-C language structure, common body and typedef type definition

1. Size judgment of structure and common body

//Structure, accounting for 8 bytes
struct A
{
	int a;
	float b;
};
   
//Union (also known as common body), accounting for 4 bytes
union B
{
	int a;
	float b;
};

Design model of structure and common body

2. Structure

Structure: struct
typedef: type definition, which defines the type

typedef application: ① give a short alias; ② give an alias according to the data characteristics (such as size_t,time_t and other data types that can be known by name)

① Usage: typedef type name new type name;

typedef unsigned long long int uint64;//Give unsigned long int
//An alias is uint64. 64 means 64 bits, which is 8 bytes, and long integer is 8 bytes

int main()
{
	unsigned long long int a = 1000;
	unsigned long long int b = 2000;
	uint64 c = 300;
}

② When we remove typedef from typedef unsigned long long int uint64, uint64 is a variable defined by unsigned long long int. after adding typedef, uint64 is promoted from variable to a data type, because typedef defines type

typedef unsigned long long int uint64;
//For example:
typedef int Arr[10];//Arr is an integer array data type with a length of 10,
//The variable defined by Arr is an integer array of 10 lengths
int main()
{
	Arr a;
	printf("a=%d\n",a);//The size of a printed out is 40 bytes, so a is an integer array of 10 lengths
}

③ In the following program, if struct is not added to the main function, an error will be reported in C language. The compiler cannot recognize what Student is, but in C + +, struct can not be written
Add typedef struct Student in C language; After this sentence, when we don't write struct, the compiler will not report an error, because Student is struct Student

struct Student
{
	char name[20];
	int age;
};

//typedef struct Student Student;

int main()
{
	struct Student stu1 = {"caocao",20};
	printf("%s %d\n",stu1.name,stu1.age);
}

④ We can also directly add typedef when defining struct, which is equivalent to
typedef struct Student Student; This method is used a lot

typedef struct Student
{
	char name[20];
	int age;
}Student;

3. Common body:
Also known as a consortium, all members share memory, which means that all members start from the same low address

In the same common variable, only one member can be used (for example, when variable a is used, b cannot be used), otherwise there will be problems with the data

union B ua;
ua.a = 10;
ua.b = 12.5;
printf("%d\n",ua.a);//The value of the printed result ua.a is no longer 10
//Therefore, you cannot use ua.b after using ua.a, otherwise there will be problems with the data of ua.a
//In the same variable (ua), two members (ua.a and ua.b) are not allowed to be used at the same time

4. (Alibaba written test question) judge which of the following 1234 statements is correct

123 is correct, 4 is wrong

Because typedef defines a type and only gives the type an alias called Pint, a and b defined by Pint are int *, so 1 and 2 are correct

#define is a macro. A macro is a character replacement. It just replaces the place where Pint is used with int *, that is, int *c,d. in this way, the defined c is an int type, but the defined d is an int type. This number only modifies c, not d

typedef int* Pint;//int* == Pint
#define Pint int * / / macro is character substitution

int main()
{
	Pint a,b;
	PINT c,d;//int *c,d
	int e;
	//a = &e;//1
	//b = &e;//2
	//c = &e;//3
	//d = &e;//4//error
	d = 40;//d is an int, and this statement is legal
}

5. Bit field (also called bit field)

Bit segment is a problem left over by history. It is no longer used. Before, the memory was relatively small and there was a bit segment problem
Because the storage order of variables in bit segments is not fixed, different compilers may have differences, resulting in poor code portability. Bit operations can replace bit segments, so bit segments are no longer used

Just know the number of bytes it occupies

int a:8;// Description a occupies 8 places

struct C//Bit segment
{
	int a:8;//Description a occupies 8 places
	int b:20;
	int c:4;
};//(8 + 20 + 4) / 8 = 4 bytes

The size of the bit segment must be a multiple of integer (multiple of 4), (10 + 30) / 8 = 5 bytes, greater than 4 bytes, so it is 8 bytes

struct D
{
	int a:10;
	int b:30;
}//8 bytes

Tags: C

Posted on Mon, 29 Nov 2021 05:04:57 -0500 by abgoosht