Why not use typedef for structure in C?

I am studying an application in C for low-power ARM devices, I have this piece of code in the first place

struct state { float position; int dummy; }; 

Notice how this structure does not define a new type, I also noticed that later in the code this structure is used like this

 struct state mystate; 

which is something strange and not convenient for me, and not flexible and with 1 extra useless keyword, that I can easily avoid using typedef for the structure in the first place.

This is a structure that is vital to the business logic of this application, and is also used a lot in the source code.

Is there a special reason not to use typedef with a structure that is used as a type in the end?

+4
source share
4 answers

I find typedef harmful. I think this is almost always misused. In my opinion, this is useful and useful only when the base type is completely abstracted - for example, All manipulations are carried out through functions. Otherwise, the base type is not abstracted - the user still needs to know what it is to understand the code - all that happened is that the damaged code was damaged, where the type namespace was without amplification (it is incorrectly abstracted for the type) .

In my opinion, almost all the use of typedef does not have an understanding of the problems that it brings, and how it should be used.

+9
source

A small part that was not mentioned here is from standard C.

6.7.7.3 [...] The typedef declaration does not introduce a new type, but only a synonym for the specified type.

You can use it to abstract some details, although when someone goes to extremes just for function calls, I wonder how they ever allocate memory, where size_t and the leak of knowledge that this is an unsigned int are necessary.

Given that this is only an alias, I have no problem using typedef for shortcuts, for example. to omit the optional keyword that is needed everywhere. I don't like the repeating pattern code, and its cover makes the code shorter. You just have to be a little careful not to affect clarity and readability, so I also prefer to keep namespaces clean and consistent. I think this is bad:

 typedef struct state { float position; int dummy; } state; 

and prefer something like this:

 typedef struct s_state { float position; int dummy; } t_state; 
+4
source

Using typedef or not is a personal preference. Personally, I use typedef for complex types of pointers or function pointers.

When you declare a structure, you also declare a type with it, namely struct structName . All typedef does this to pull out part of the struct definition in this script and is not needed. This has nothing to do with low power devices.

+3
source

This is mainly for clarity. In practice, it is generally understood that if you need to use the struct keyword, you must manipulate the internal data yourself. When a struct typedef'd, it hides the actual type, and the manipulation should be done using functions. This is a convention that is not always respected, while others simply leave the struct keyword to distinguish it from opaque types.

+3
source

Source: https://habr.com/ru/post/1434105/


All Articles