What is the difference between the data types enum, struct and union in C?

3

I'm reading the GNU C manual and I'm in the Data Types section and I notice a certain similarity between% types of%,% with%, and% with%. Is the syntax the same, or is there something that differs between them? In which cases should I use which?

    
asked by anonymous 27.06.2016 / 19:59

2 answers

2

Although they have similar syntaxes, their functions are very different, the union is a chameleon structure, and an enumeration has constant data.

struct

A structure has a set of members, that is, each member listed there will be part of the structure, it is additive. Its size is the sum of the members plus the alignment .

union

A union has a single data between a variety available among its members, that is, only one member will be actually used, excluding the possibility of others. Its size is the size of the largest member considering alignment.

From the example in the manual:

 union numbers {
     int i;
     float f;
 };

numbers is a join structure that will either have an integer or have a floating point data. It depends on architecture, but probably both have the same size (4 bytes, in most cases), so you can say that numbers will have 4 bytes (the right one is to always use sizeof to find out).

The union is very useful for "chatting" with the hardware since it can provide data of various types. This is a way to dynamize the die. So you can have a single data that can take many forms (types). but several APIs use it to make it easier to pass parameters or give flexibility.

It is also useful for creating a "variable type", similar to what dynamic languages usually have. This is an internal technique that these languages use for their own variables. It is a form of polymorphism. It is common for the union to be inside a structure with another member indicating which type is being saved at that time, this is called tagged union .

It has a technique that is used to assemble or unmount a die. You can for example mount a struct with 4 members of type char (1 byte therefore) and then create a union with 2 members, one is this struct , and another is int . So you can put each of the bytes in struct and then access the data by the member that is a int , it will be seen as a thing. If set up the right way, it will form a valid number. A more "correct" example:

typedef union {
    struct {
        int8_t a;
        int8_t b;
        int8_t c;
        int8_t d;
    };
    int32_t x;
} meu_tipo;

meu_tipo var;
var.a = 0xFF;
var.b = 0xC0; 
var.c = 0xA5;
var.d = 0x0F;
int32_t y = var.x;

It is possible to do this with bits instead of bytes.

enum

Enumeration is a way to use constant data (resolved at compile time) that is listed in a set. Behavior resembles union a little. An object of this kind will only have one value, the others are exclusive. In it, instead of having any given data, what is used is the member itself that has a fixed value defined in its own structure.

The data for each member is usually a numeric sequence and it is possible to do mathematical operations with them, but it is possible to give values that you want for each member. If you leave the default, the compiler considers a string starting at 0.

If there is no compiler extension the type of each member is always int and a value belonging to this enumeration can be used where a int can be used.

For many programmers this is the best way to use constants in C. Of course it depends on the goal.

Some people like to use capitalized names as they did with #define , but many programmers do not do this because they do not have the same need of the past.

enum Direcao {Norte, Sul, Leste, Oeste}; //Norte é 0 e Oeste é 3
enum Permissao {Ler = 1, Escrever = 2, Apagar = 4, Executar = 8}
int p = Ler | Escrever; //vale 3 indicando que estas duas permissões estão ativas
    
27.06.2016 / 20:50
1

ENUM

It's quite different from the rest, since it's a list of numbers with easier to use names.

For example, you can create the list with both types below:

typedef enum 
{
    TIPO_NM = 0,  //numero
    TIPO_ST       //string
}TIPO_VARIAVEL

So you can make a switch, for example, by treating each of the enum instances and dealing with the received types properly. The program will consider the number 0 for TYPE_NM and 1 for TYPE_ST, making it easier to read what each type means by one person. The more types you have, the more important it is to have an enum.

STRUCT

This is the key word for variable structures. Any set can be represented here.

For example, in the structure below a message is represented, imagining that all the messages of a certain communication always have 10 bytes of message body, an integer of crc and a key length that identifies the message:

typedef struct
{
    char mensagem[10];
    int crc;
    long chave;
}Mensagem

UNION

It means that in the same structure, it can be used one way or another. This is to save space and the bytes are interpreted differently in each case of the union.

For example, if the above struct is used as a union, it might be a number or string message depending on the long received. So the interpretation would be different in each case, but the same structure would be fed whenever you get a new message.

    
27.06.2016 / 21:14