What is the typedef function in struct in C? Can I use struct without it?

Asked

Viewed 366 times

4

I’m studying data structure in C, and every time my teacher uses the typedef us struct, but I don’t understand his function.

  • Could I put the example of struct in the question?

  • to create an adjacent matrix using graph, e.g.: typedef struct{ int matrix[MAX][MAX] int numeroDeVertices};

  • @Rennanreis The answer solved your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

2 answers

7

It’s not just for struct. The typedef is used to define a data type, as the name already indicates.

In addition to the existing types in the language you can create your own and function like any other.

A defined type can be something very simple, as just a different name (an abstraction) for an existing type, as it can be the composition of some values in a given format that create a value form different from what exists.

You can do this with the struct, But if you just do it, it doesn’t create a new type for the language to use with an available name whenever you load it. It will still create a new type, but it will be considered anonymous for the language, it will have a name only for the command struct, which is boring to use and may cause some misunderstandings. With the typedef makes everything easier because it makes it simple, clean and correct. Basically it’s an abstraction.

It is often recommended to use the uppercase initial of the name to avoid conflicting with other symbols, or to use a suffix _t. Something like that:

typedef struct { int x; int y; } Ponto;

Then you use in the declaration of a variable:

Ponto ponto;

Or cast:

(Ponto)ponto

Without him:

struct ponto ponto;

Or:

(struct ponto)ponto

I put in the Github for future reference.

When you use a name with struct the name pattern is less important because it is certainly a type, it cannot be used, and therefore confused, with other code identifiers. When you use a typedef to struct does not need to have a name. It can have a struct anonymous without typedef, but then it can’t be used anywhere else in the application.

  • "But then it can’t be used anywhere else in the application." means that if I create a typedef in a header file and don’t set the name for the struct, I can’t call that type in any file. c that is including this header file?

  • 1

    Not that, take the whole sentence: Pode ter uma struct anônima **sem typedef**, mas aí ela não poderá ser usada em lugar algum mais na aplicação.

  • Ahhhhhhhh, now I get it, I was reading it wrong, thank you

-2

You see, the typedef need not necessarily be used with structs.

One struct defines a new data type, a record that can contain information of various data types at the same time, as in the example below:

struct pessoa {
     char nome[50];
     int idade;
     float altura;
};

To create a variable of type person in main, we could do it this way:

int main() {
    struct pessoa p;
}

See that we would have to carry the reserved word struct every time we refer to some variable with the defined type. That’s where the typedef, which literally would be a "type definition".

Basically, its function is to create a "nickname" for data types, allowing you not to write that data type every time you call a variable of this type, and this is valid for structs also. Let’s look at the previous example, imagine that we want to refer to struct pessoa only as Person:

typedef struct pessoa {
     char nome[50];
     int idade;
     float altura;
} Pessoa;

The code excerpt above basically says that we want to call the struct pessoa of Person. Now, to declare a variable of type struct pessoa, we can do it this way:

int main() {
     Pessoa p;
}

In short, the typedef allows you to associate a name to a data type, and can make your code more readable and streamlined.

Browser other questions tagged

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