This is the 26th day of my participation in the First Challenge 2022

I hope our c++ sharing is interesting and comprehensive. Last time we shared structs and this time we will introduce union. Before introducing union, we will briefly review structs and then introduce what they have in common and what the differences are.

#include <stdio.h>
#include <stdbool.h>

int main(a)
{}Copy the code

You might prefer to define a struct this way with a typedef followed by an anonymous struct even though you give that struct a name

typedef struct {
	int x;
	float y;
	char z;
} mystruct;
Copy the code

Creating a union is very similar to creating a struct

typedef union {
	int x;
	float y;
	char z;
} mynuion;

Copy the code
int main(a)
{
	printf("struct size: %lu\n".sizeof(mystruct));
	printf("union size: %lu\n".sizeof(mynuion));
}
Copy the code

By printing the size of mystruct and myUnion, it is not difficult to find that the memory size allocated to struct is the sum of the memory size of all its members, while for union, it is only the memory size occupied by one member

By the way, there are two visual Studio shortcuts: CTRL + D to copy a line, Alt + ARROW keys to move up and down

struct size: 12
union size: 4
Copy the code

Now let’s instantiate the struct and the Union using the usual data and then output their members separately to see how they differ

int main(a)
{
	mystruct s;
	s.x = 2;
	s.y = 3.1;
	s.z = 'Z';


	myunion u;
	u.x = 2;
	u.y = 3.1;
	u.z = 'Z';


	printf("struct size: %lu\n".sizeof(mystruct));
	printf("{%d, %f, %c}\n", s.x, s.y, s.z);
	printf("union size: %lu\n".sizeof(myunion));
	printf("{%d, %f, %c}\n", u.x, u.y, u.z);
}
Copy the code

From the following output, it is clear that the union remains unchanged except for Z. The previous assignments x and y change, because each assignment overwrites the previous content because the union has only one memory space. In this case, why do we need the union? Here is an example to explain it to you.

struct size: 12
{2.3.100000, Z}
union size: 4
{1078355546.3.099997, Z}
Copy the code

Define tuT structure, this tuT structure has two types, can be video tut or text tut, through isVideo to distinguish, when the video tuT has a level to distinguish short video or long video, but text, A description attribute is required, meaning that description and Level belong to different categories

typedef struct {
	char* title;
	bool isVideo;
	char* description;
	int level
} tut;
Copy the code

This is where union comes in handy, because each instantiation of a struct can only be one type of text or video, and description and level belong to different types, so we can use other unique properties of union to solve this problem, as follows

typedef struct {
	char* title;
	bool isVideo;
	union
	{
		char* description;
		int level;
	};
} tut;

void print_tut(tut* t)
{
	printf("Tut: %s " t->title);
	if ( t->isVideo) {
		printf("video tut %i", t->level);
	}
	else
	{
		printf(" tut %c", t->description); }}Copy the code

Let’s show the complete code


typedef struct {
	char* title;
	bool isVideo;
	union
	{
		char* description;
		int level;
	};
} tut;

void print_tut(tut* t)
{
	printf("Tut: %s\n",t->title);

	if ( t->isVideo) {
		printf("video tut %i\n", t->level);
	}
	else
	{
		printf(" tut %c\n", t->description); }}int main(a)
{
	mystruct s;
	s.x = 2;
	s.y = 3.1;
	s.z = 'Z';


	myunion u;
	u.x = 2;
	u.y = 3.1;
	u.z = 'Z';


	//printf("struct size: %lu\n", sizeof(mystruct));
	//printf("{%d, %f, %c}\n", s.x, s.y, s.z);
	//printf("union size: %lu\n", sizeof(myunion));
	//printf("{%d, %f, %c}\n", u.x, u.y, u.z);

	tut machineLearningTut;
	tut deepLearnTut;

	machineLearningTut.title = "machine learning";
	machineLearningTut.isVideo = false;
	machineLearningTut.description = "machine learning ";

	deepLearnTut.title = "deep learning";
	deepLearnTut.isVideo = true;
	deepLearnTut.level = 1;

	print_tut(&machineLearningTut);
	print_tut(&deepLearnTut);
}
Copy the code