Difference between Struct and an incomplete Struct type

1

When declaring these two struct, what are their differences? Do the fields change from where they can be invoked?

Struct general:

typedef struct Etapa{
  int h, m, s;
}Etapa;                       

Etapa etapas[3];
Etapa *puntero_etapa = etapas;

Struct anonymous:

typedef struct{
  int h, m, s;
}Etapa;                       

Etapa etapas[3];
Etapa *puntero_etapa = etapas;
    
asked by Diego 14.09.2017 в 16:35
source

1 answer

3

The second case is not an incomplete struct but, as you indicate later, anonymous.

The only difference between both is that the second can only be created using the alias:

Etapa variable;
struct /* ¿Que ponemos aqui? */ variable2; // <-- no compila

While in the first case you can create the structure using its name or alias:

Etapa variable;
struct Etapa variable;

Any advantages of having structures with a name?

The only one that occurs to me is that if a structure has no name you can not declare pointers to it within the structure itself ... something necessary to create linked lists ...

struct Nodo
{
  struct Nodo* sig;
};

Of course, the alias does not need to be declared at the same time as the structure is declared ... it is usually more readable to declare it in an independent line:

struct Etapa
{
  // ...
};

typedef struct Etapa Etapa;
    
answered by 14.09.2017 / 16:38
source