C a consortium

The target

In this article, you’ll learn about a new type called C-union and how to use it effectively in your programs.

What is the C union?

Structs allow you to define new data types with multiple related fields. Each field occupies a separate storage location. Such as:

struct point
{
    int x;
    int y;
};
Copy the code

The point structure has two fields, x coordinate and y coordinate. Each occupies a separate space in memory.

A union is similar to a structure. However, it defines a place to store the values of different fields at a single point in time.

union quantity {
    int i_value;
    float f_value;
}
Copy the code

In this quantity federation, the I_value and F_value fields share the same memory location.

The following picture illustrates the difference between a structure and a union:

By definition, union is a type that stores different values in the same memory location but not at the same time. A federation is a group of data objects that share a single block of memory.

C union syntax

The syntax for defining unions is similar to the syntax for defining struct types. The syntax for defining unions is shown below:

Union union name {type field name; Type field name; / /... };Copy the code

In this syntax:

First, you start with the union keyword, followed by the union name. Second, use a type to specify fields. To access members of a consortium, use (.) like this Operator:

Consortium name. Field nameCopy the code

Union and structure

In a structure, each field stores data separately. If you change the value of one field in a structure, the values of the other fields do not change.

However, in a union, all fields share the same block of memory. This memory block is large enough to store the values of the largest fields. Smaller members use as much memory as possible. If you change the value of one field, the value of the other fields will also change.

If you need to store data in members at the same time, you need to use a structure.

Initializing the union

C allows you to initialize a union in two ways:

Initialize a union by initializing its first member. Or initialize a union by assigning it to another union of the same type. The following program shows how to initialize a union in two ways.

#include <stdio.h> #include <stdlib.h> int main() { typedef union { char ch; int flag; float f; } data; data d; d.ch = 'A'; // initialize data d2 = d with another union; // initialize the first member of the union data d3 = {'B'}; return 0; }Copy the code

C union example

In this case, we have an account structure, which can be a personal account or a Business account based on the Account_type enumeration. If it is a personal account, the INFO member is associated with the Person structure, otherwise it is associated with the Company structure.

Just take a few minutes to walk through the code to better understand the use of C federation.

#include <stdio.h> #include <stdlib.h> // Define account type: personal or business enum account_type {personal = 1, business = 2}; Struct person {char *name; }; Struct company {char *name; struct company {char *name; char *tax_no; }; Typedef union {struct person individual; struct company biz; } profile; // typef struct {char *username; char *password; enum account_type type; profile info; } account; void display(account acc); Int main() {printf(" union example \n"); account acc1, acc2; acc1.type = personal; acc1.username = "acc1"; acc1.password = "secret"; acc1.info.individual.name = "xiaowang"; display(acc1); acc2.type = business; acc2.username = "acc2"; acc2.password = "secret2"; acc2.info.biz.name = "My Company"; acc2.info.biz.tax_no = "112121"; display(acc2); return 0; } /* Display the account */ void display(account acc) {switch (acc.type) {case personal: printf(" personal account \n"); Printf (" username :%s\n Name :%s\n", acc.username, acc.info.individual.name); break; Case Business: printf(" business account \n"); Printf (" Username :%s\n Company :%s\n Tax number :%s\n", acc.username, acc.info.biz. Name, acc.info.biz. Tax_no); break; } printf("-------------------------------\n"); }Copy the code

conclusion

  • In this article, you’ve learned how to use C-unions and understood the difference between unions and constructs.