Fundamentals of C Language: Structures and Unions

In learning the C language, arrays allow us to efficiently manage data of the same type, but real-world data often consists of a combination of different types, such as a student’s information which includes name (string), age (integer), and score (float). At this point, we need a more flexible and powerful tool to organize this data — that is the structure (struct).

In addition, C language also provides a special data type — union, which can store different data types in the same memory space. Although its usage is relatively rare, it has irreplaceable value in certain low-level development, memory optimization, protocol parsing, and other scenarios.

In this article, we will delve into the structures and unions in C language, covering their definitions, initialization, usage methods, typical application scenarios, and precautions, helping you master this important data organization tool and laying a solid foundation for subsequent data structures and system programming.

1. Structure (struct)

A structure is a user-defined data type that can combine different types of data into a whole. Structures are commonly used to represent a data object with multiple different attributes, such as a student’s data, which may include name, age, and score.

1. Declaring a Structure

struct Person {    char name[50];    int age;    float height;};

Here we define a Person structure, which contains three members: name (string), age (integer), and height (float). The members of a structure can be of any data type.

2. Creating Structure Variables

Once the structure is defined, we can declare structure variables as follows:

struct Person person1;

3. Accessing Structure Members

You can use the dot operator . to access structure members:

person1.age = 26;strcpy(person1.name, "grey");person1.height = 100.1;

4. Initializing Structures

Structures can also be initialized at the time of declaration:

struct Person person1 = {"grey", 25, 100.1};

5. Using Structure Pointers

Structure pointers are used to access the members of a structure and require the use of the arrow operator -> :

struct Person *ptr = &person1;ptr->age = 26; // Modify structure member using pointer

6. Structure Arrays

We can also declare an array of structures to store multiple structure objects:

struct Person people[2] = {    {"grey",26,100.1},    {"gray",26,100.2}};

2. Union (union)

Unions are very similar to structures, with the difference being that the members of a union share a segment of memory. That is, all members of a union occupy the same memory space, with the size determined by the largest member. Each member of a structure occupies independent memory space.

1. Declaring a Union

union Data {    int i;    float f;    char str[20];};

Here we define a union Data, which has three members: an integer i, a float f, and a character array str.

2. Using a Union

We can use union variables just like structure variables, but we must note that all members of a union share the same memory:

union Data data;data.i = 10;printf("%d\n", data.i);data.f = 100.1;printf("%f\n", data.f);

3. Size of a Union

The size of a union is equal to the size of its largest member. You can check the size of a union using sizeof:

printf("Union size : %lu\n", sizeof(union Data));

3. Differences: Structures and Unions

· Structure: Each member occupies independent memory, all members can be used simultaneously.· Union: All members share the same memory, only one member can store data at the same time.

Structures are the foundation for implementing custom composite data types in C language, allowing us to organize logically related but type-different data together, greatly enhancing the readability and maintainability of the code, and serving as an important cornerstone for subsequent learning of linked lists, trees, file processing, network communication, and other knowledge.

Unions may not be used as frequently as structures, but they play a unique role in certain specific scenarios (such as memory optimization, type reinterpretation, embedded development) through shared memory, and are a key part of understanding the C language memory model.

I hope you can flexibly use structures to organize your data in your future coding practices, understand the memory-sharing characteristics of unions, and choose wisely when needed. The essence of data structures is the reasonable organization of data — and structures are an important tool for you to achieve this goal! Keep it up, comrades!

Leave a Comment