What is the utility of the struct command in c?

1

I've already learned how to use the command in a code, but I still do not understand exactly what it's for. Could someone explain me, with examples if possible, its usefulness?

    
asked by anonymous 24.08.2018 / 06:36

1 answer

6

The struct is basically used to group variables that have a common goal and create new data types. Technically, the struct will physically align this data in memory allowing it to be accessed by a single access point. For example: If you need to store a person's data such as 'name', 'age', 'gender', etc., define a struct called 'person' with the data you need.

struct pessoa {
  char *nome;
  unsigned int idade;
  char genero[1]; // M ou F
};

// Declaração da variável p utilizando a struct pessoa
struct pessoa p;

// Definição dos valores. 
p.nome = "Joao";  
p.idade = 12;
p.genero = 'M';
    
24.08.2018 / 07:26