I have seen many programs consisting of structures like the one below
typedef struct
{
int i;
char k;
} elem;
elem user;
Why is it needed so often? Any specific reason or applicable area?
I have seen many programs consisting of structures like the one below
typedef struct
{
int i;
char k;
} elem;
elem user;
Why is it needed so often? Any specific reason or applicable area?
It turns out that there are pros and cons. A useful source of information is the seminal book "Expert C Programming" (Chapter 3). Briefly, in C you have multiple namespaces: tags, types, member names and identifiers.
typedef
introduces an alias for a type and locates it in the tag namespace. Namely,defines two things. One Tag in the tag namespace and one Type in the type namespace. So you can do both
Type myType
andstruct Tag myTagType
. Declarations likestruct Type myType
orTag myTagType
are illegal. In addition, in a declaration like this:we define a pointer to our Type. So if we declare:
then
var1
,var2
andmyTagType1
are pointers to Type butmyTagType2
not.In the above-mentioned book, it mentions that typedefing structs are not very useful as it only saves the programmer from writing the word struct. However, I have an objection, like many other C programmers. Although it sometimes turns to obfuscate some names (that's why it is not advisable in large code bases like the kernel) when you want to implement polymorphism in C it helps a lot look here for details. Example:
you can do:
So you can access an outer member (
flags
) by the inner struct (MyPipe
) through casting. For me it is less confusing to cast the whole type than doing(struct MyWriter_ *) s;
every time you want to perform such functionality. In these cases brief referencing is a big deal especially if you heavily employ the technique in your code.Finally, the last aspect with
typedef
ed types is the inability to extend them, in contrast to macros. If for example, you have:you can then declare
We do not really care for this for structs because it does not apply to storage specifiers (
volatile
andconst
).Using a
typedef
avoids having to writestruct
every time you declare a variable of that type:At all, in C language, struct/union/enum are macro instruction processed by the C language preprocessor (do not mistake with the preprocessor that treat "#include" and other)
so :
struct b is expended as something like this :
and so, at compile time it evolve on stack as something like: b: int ai int i int j
that also why it's dificult to have selfreferent structs, C preprocessor round in a déclaration loop that can't terminate.
typedef are type specifier, that means only C compiler process it and it can do like he want for optimise assembler code implementation. It also dont expend member of type par stupidly like préprocessor do with structs but use more complex reference construction algorithm, so construction like :
is permited and fully functional. This implementation give also access to compilator type conversion and remove some bugging effects when execution thread leave the application field of initialisation functions.
This mean that in C typedefs are more near as C++ class than lonely structs.
Linux kernel coding style Chapter 5 gives great pros and cons (mostly cons) of using
typedef
.It's amazing how many people get this wrong. PLEASE don't typedef structs in C, it needlessly pollutes the global namespace which is typically very polluted already in large C programs.
Also, typedef'd structs without a tag name are a major cause of needless imposition of ordering relationships among header files.
Consider:
With such a definition, not using typedefs, it is possible for a compiland unit to include foo.h to get at the
FOO_DEF
definition. If it doesn't attempt to dereference the 'bar' member of thefoo
struct then there will be no need to include the "bar.h" file.Also, since the namespaces are different between the tag names and the member names, it is possible to write very readable code such as:
Since the namespaces are separate, there is no conflict in naming variables coincident with their struct tag name.
If I have to maintain your code, I will remove your typedef'd structs.
In 'C' programming language the keyword 'typedef' is used to declare a new name for some object(struct, array, function..enum type). For example, I will use a 'struct-s'. In 'C' we often declare a 'struct' outside of the 'main' function. For example:
Each time I decide to use a struct type I will need this keyword 'struct 'something' 'name'.'typedef' will simply rename that type and I can use that new name in my program every time I want. So our code will be:
If you have some local object(struct, array, valuable) that will be used in your entire program you can simply give it a name using a 'typedef'.