Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should we typedef a struct so often in C?

Tags:

c

struct

typedef

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?

like image 622
Manoj Doubts Avatar asked Oct 31 '08 07:10

Manoj Doubts


People also ask

Why do we use typedef struct in C?

The C language contains the typedef keyword to allow users to provide alternative names for the primitive (e.g.,​ int) and user-defined​ (e.g struct) data types. Remember, this keyword adds a new name for some existing data type but does not create a new type.

What is the advantage of using typedef with a structure?

Typedefs provide a level of abstraction away from the actual types being used, allowing you, the programmer, to focus more on the concept of just what a variable should mean. This makes it easier to write clean code, but it also makes it far easier to modify your code.

What is difference between struct and typedef struct?

Basically struct is used to define a structure. But when we want to use it we have to use the struct keyword in C. If we use the typedef keyword, then a new name, we can use the struct by that name, without writing the struct keyword.

Is typedef a good practice?

No, and it is often used in classes — especially template classes — to define a class-dependent type used throughout the class so that knowledge of the underlying types is not necessary.


2 Answers

As Greg Hewgill said, the typedef means you no longer have to write struct all over the place. That not only saves keystrokes, it also can make the code cleaner since it provides a smidgen more abstraction.

Stuff like

typedef struct {   int x, y; } Point;  Point point_new(int x, int y) {   Point a;   a.x = x;   a.y = y;   return a; } 

becomes cleaner when you don't need to see the "struct" keyword all over the place, it looks more as if there really is a type called "Point" in your language. Which, after the typedef, is the case I guess.

Also note that while your example (and mine) omitted naming the struct itself, actually naming it is also useful for when you want to provide an opaque type. Then you'd have code like this in the header, for instance:

typedef struct Point Point;  Point * point_new(int x, int y); 

and then provide the struct definition in the implementation file:

struct Point {   int x, y; };  Point * point_new(int x, int y) {   Point *p;   if((p = malloc(sizeof *p)) != NULL)   {     p->x = x;     p->y = y;   }   return p; } 

In this latter case, you cannot return the Point by value, since its definition is hidden from users of the header file. This is a technique used widely in GTK+, for instance.

UPDATE Note that there are also highly-regarded C projects where this use of typedef to hide struct is considered a bad idea, the Linux kernel is probably the most well-known such project. See Chapter 5 of The Linux Kernel CodingStyle document for Linus' angry words. :) My point is that the "should" in the question is perhaps not set in stone, after all.

like image 174
unwind Avatar answered Sep 19 '22 13:09

unwind


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:

#ifndef FOO_H #define FOO_H 1  #define FOO_DEF (0xDEADBABE)  struct bar; /* forward declaration, defined in bar.h*/  struct foo {   struct bar *bar; };  #endif 

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 the foo 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:

struct foo *foo;  printf("foo->bar = %p", foo->bar); 

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.

like image 26
Jerry Hicks Avatar answered Sep 17 '22 13:09

Jerry Hicks