What is the correct way to declare a struct in C?

Asked

Viewed 2,845 times

4

Also how to rename data types with structures?

I have doubt about it because of a Windows Manager that I use has the following code:

typedef struct exem exem;

struct exem {
    tiposdedado variavel;
};
  • What is wm? What do you call renaming? Are you having a problem? That’s essentially the way to declare the structure. Do you have any questions? Give more context to what you’re trying to ask.

  • window manager, I think I wrote wrong is to assign a variable to the data types allowed in the language c

  • 1

    You can [Dit] the question and clarify better than it questions. I have an interest in answering it, but I’m not sure what you want to know. For me there is a question here, yet.

  • in this same wm I use has 2-mode 1 declared structures which I mentioned above and 2 is typedef struct { variable typed; }ex; in the first mode, within the main function I can call by the name of the structure ex: ex Ex1; o in the second I cannot call inside the main function without calling struct before ex: struct ex Ex1;

1 answer

4


Statement of structure

struct exem {
    tipoDeDado variavel;
};

It is declaring the data structure, it will have the name exem. It can be used in any part of the application as long as it is available, that is, it has to be in that same file or that statement need to be included where used (another reference). The structure name identifier is considered to be only one tag.

To declare some value or variable that conforms to this structure you will have to say that you are defining a struct. Thus:

struct exem var;

Using that way your type will be a struct who will have a tag to differentiate from existing ones, the identifier does not work without the keyword.

When you use struct tagName the compiler considers it to be declared elsewhere, so some errors may not be picked up that way. There are times when this is useful, but in most of them it is something that gives less robustness to the code. See how it compiles something that doesn’t even exist. A typo won’t even get caught and sometimes it’s a bug crabby.

Type declaration

typedef struct exem exem;

You are now creating a new type of data. You are declaring its name and how it is composed. Its identifier is a type name. The name of the type is now independent.

In case he’s saying exem (the last) is the name of the type that happens to exist and it will be composed by tag exem (the first that can only be used together with struct, as already seen above). In this example has the same name but are different things. This is the same as doing so:

typedef struct exem {
    tipoDeDado variavel;
} exem;

So in a single statement we created the tag and the type of data. I find it simpler to do this. There are cases that it is interesting to have separated.

Together or separately, 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. Would look like this:

exem var;

A common thing that people do to avoid confusion and create a convention for data types is to change the name a little, note the capital of the data type:

typedef struct exem {
    tipoDeDado variavel;
} Exem;

So it’s even possible to do this in the declaration of variables of this type:

Exem exem; //exem é uma variável normal

Note that the identifier exem does not confuse with the tag, after all the tag exists only as the name of struct.

There are those who prefer to capitalize on both. I already say below because the capital is important (by convention).

It’s even normal not to use a name tag and just name the guy:

typedef struct {
    tipoDeDado variavel;
} Exem;

I put in the Github for future reference.

You need to understand that creating types without need can pollute the namespace general. When using the struct he’s kind of protecting the necessary names, those names only exist in that context, so it allows you to have a struct with a name and identifiers of the same name.

Hence the type convention is capitalized for the type identifier, normally not used in general identifiers.

typedef is a more robust solution with a disadvantage that can be easily bypassed, so I prefer it whenever I can.

Forward declaration

Note that in the question example it is declaring something that does not even exist yet, this is called forward declaration, Of course, it’s more useful when you’re a little further away from each other and I have a need, but I imagine that’s an abstract example. It is useful for cases that you need to use a type name that will only be defined later, probably by having 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 within the structure itself it would give error because it has not yet been completely defined. Or you’d have to make a forward declaration or use the form of struct tagName.

More correct

There is no more correct, there is taste or some situation that it is interesting to do separated by semantic questions. You may have more than one type with the same structure, probably this may change in the future, so you need the separate statement, a structure cannot declare inline more than one type or make such a forward declaration already cited.

I gave the parameters to decide the most correct in each situation.

Note that this changes somewhat in C++, but is not the focus of the question.

  • thanks for the information

Browser other questions tagged

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