typedef struct syntax

2

What is the difference between writing typedef struct "nome1"{}"NOME2"; , as implemented in a linked list; and typedef struct{}"NOME1"; , as implemented in the sequential list I've seen.

I've also come across struct "nome1"{}typedef struct "nome1" "nome2"; . Is there a standard for writing the name in the uppercase?

Sequential listing:

typedef struct{
  char nome[30];
  char posicao[30];
  int  nrCamisa;
}JOGADOR;

typedef struct{
  JOGADOR vetor[MAX];
  int NroElem;
}ELENCO;

List simply chained:

typedef struct{
  char nome[30];
  char posicao[30];
  int  nrCamisa;
}JOGADOR;

typedef struct no{
  JOGADOR info;
  struct no* prox;
}ELENCO;
    
asked by anonymous 19.08.2017 / 00:04

1 answer

2

typedef creates a new type, it has nothing to do with the structure. You can do this:

typedef int Inteiro

You are creating a new type called Inteiro which will be based on type int .

Then you can also:

struct exemplo {
    int x;
}

typedef struct exemplo Exemplo;

What is the same thing to do:

typedef struct exemplo {
    int x;
} Exemplo;

Then there exemplo is the name of the structure, and Exemplo is the name of the type.

Note that I prefer to use type names beginning with uppercase or CamelCase. There are those who like ALL CAPS, there are those who like everything tiny, there is no universal standard.

A structure need not have a name. Of course, if it has no name or it can only be used once or it will have to be set to a typedef which will then be used to create a data based on the anonymous structure.

I am not particularly fond of naming the structure in typedef . Unless the type itself is used inside the structure, it creates a problem there, because the type can not be used in a structure that defines it, the type does not yet exist when the structure is being defined. So in the last example has the name because of this. The structure references itself before setting ELENCO , so this name can not be used, you must use struct no .

Note that the structure name only exists in the scope of a struct while the type name is global.

    
19.08.2017 / 00:31