Also how to rename struct data types?
I have doubt about this because of a Windows Manager that I use has the following code:
typedef struct exem exem;
struct exem {
tiposdedado variavel;
};
Also how to rename struct data types?
I have doubt about this because of a Windows Manager that I use has the following code:
typedef struct exem exem;
struct exem {
tiposdedado variavel;
};
struct exem {
tipoDeDado variavel;
};
You are declaring the data structure, it will be named exem
. It can be used in any part of the application as long as it is available, that is, it must be in the same file or this statement needs to be included where it is used ( another reference ). The structure name identifier is considered only a tag .
To declare some value or variable that conforms to this structure you have to say that you are setting struct
. So:
struct exem var;
Using this way your type will be a struct
that will have a tag to differentiate from existing ones, the handle does not work without the keyword.
When using struct tagName
the compiler considers that it is declared elsewhere, so some errors may not be caught that way. There are times when this is useful, but in most of them it is something that gives the code less robustness. See how to compile something that does not even exist . A typo is not even picked up and sometimes it's a boring bug.
typedef struct exem exem;
You are now creating a new data type. You are declaring his name and how it is composed. Your identifier is a type name . The type name becomes independent.
In case it is saying that exem
(the last) is the type name that happens to exist and it will be composed by tag exem
(the first that can only be used together with struct
, as you saw above). This example has the same name but different things. This is the same as doing so:
typedef struct exem {
tipoDeDado variavel;
} exem;
So in a single statement we create the tag and the data type. I think it's simpler to do that. There are cases that are interesting to have separated.
Together or separate, the advantage of using this form is that the statement can be simplified since you have a data type and not just a tag for struct
. It would look like this:
exem var;
One common thing that people do to avoid confusion and create a convention for data types is to change the name a bit, note the uppercase of the data type:
typedef struct exem {
tipoDeDado variavel;
} Exem;
So you can even do this in the declaration of such variables:
Exem exem; //exem é uma variável normal
Note that the identifier exem
does not confuse with the tag , after all the tag only exists as the name of struct
.
Some people prefer to use uppercase in both. I already say below because the capital letter is important (by convention).
It's even normal to use a tag name and only give the name to the type:
typedef struct {
tipoDeDado variavel;
} Exem;
You need to understand that creating unnecessary types can pollute the general namespace . When using struct
it kind of is protecting the necessary names, these names only exist in that context, so it allows to have a struct
with a name and identifiers with the same name.
typedef
is a more robust solution with a drawback that can be easily circumvented, so I prefer it whenever I can.
Note that in the example question he is declaring something that does not even exist yet, this is called forward declaration in>>, of course it is most useful when they are a little further from each other and I have a need, but I imagine this to be an abstract example. It is useful for cases that you need to use a type name that will only be defined later, probably because it has circular reference.
In linked lists, trees, or others that have references to the structure itself, it is common to define the tag and the type name. If you used the type name inside the structure itself would give error because it has not yet been completely defined. Either you would have to do a forward declaration or use the form of struct tagName
.
There is no more correct, there is taste or some situation that is interesting to do separated by semantic issues. You may have more than one type with the same structure, this may probably change in the future, so you need the separate statement, a structure can not declare more than one type or make inline forward declaration .
I gave the parameters to decide the most correct in each situation.
Note that this changes a bit in C ++, but it is not the focus of the question.