Struct definition in C

Asked

Viewed 463 times

2

Hello, my teacher has made the following definition of some structs for a list code in data structure, but unfortunately I cannot understand the definition of each struct itself.

typedef struct{
    int Chave;
}TipoItem;

typedef struct TipoCelula *TipoApontador;// Maior dúvida

typedef struct TipoCelula{
    TipoItem Item;
    TipoApontador Prox;
}TipoCelula;

typedef struct TipoLista{
    TipoApontador Primeiro, Ultimo;
}TipoLista;

I could not understand mainly the line commented, because from what I understood he defined a pointer that points to a struct Tipocelula that had not even been created!

2 answers

3


Your teacher did not "defined a pointer that points to a struct TipoCelula" he named a symbol for the type struct TipoCelula* using typedef.

Stay tuned, statement is not the same thing as definition!

The typedef is very useful to give symbolic names for data types.

To better understand what happens, we will "remove" the typedefs of your code, it would look something like this:

struct Item
{
    int Chave;
};

struct Celula
{
    struct Item item;
    struct Celula * Prox;
};


struct Lista
{
    struct Celula * Primeiro;
    struct Celula * Ultimo;
};

Now, let’s create symbolic names for each of the structures defined using typedef, only that way separate, rewriting everything would look something like:

typedef struct Item   TipoItem;
typedef struct Celula TipoCelula;
typedef struct Lista  TipoLista;

struct Item
{
    int Chave;
};

struct Celula
{
    TipoItem Item;
    TipoCelula * Prox;
};


struct Lista
{
    TipoCelula * Primeiro;
    TipoCelula * Ultimo;
};

Pointers to types can also be named symbolically using typedef, this is one of the techniques used by your teacher that you were not able to understand, look at this:

typedef struct Item   TipoItem;
typedef struct Celula TipoCelula;
typedef struct Lista  TipoLista;

typedef struct TipoCelula* TipoApontadorCelula;


struct Item
{
    int Chave;
};

struct Celula
{
    TipoItem Item;
    TipoApontadorCelula Prox;
};


struct Lista
{
    TipoApontadorCelula Primeiro;
    TipoApontadorCelula Ultimo;
};

2

What’s the problem with using a type that hasn’t been defined yet? You don’t need its definition, just exist. Can not use before setting when use depends on setting, in this case only need the name.

Let’s make it clear what you’re doing:

typedef (struct TipoCelula *) TipoApontador;

So this line is creating a new type called TipoApontador. This type will be a pointer to a structure TipoCelula.

When you’re using:

TipoApontador Prox;

You’re actually doing:

struct TipoCelula *Prox;

I put in the Github for future reference.

  • But in this definition of functions, Tipolista is a pointer struct, so what differentiates these two definitions of functions: void Flvazita(Typolista *Lista); int Empty(Tipolista Lista);

  • I didn’t see any definition of function there. TipoLista is a two-member structure, both are the type TipoApontador (I find these names horrible).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.