Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

in C (ANSI, C99, etc.), structs live in their own namespace. A struct for a linked list might look something like this:

struct my_buffer_type {
   struct my_buffer_type * next;
   struct my_buffer_type * prev;
   void * data;
};

It seems quite natural however for most C programmers to automatically typdef those structs like the following

typedef struct tag_buffer_type {
   struct my_buffer_type * next;
   struct my_buffer_type * prev;
   void * data;
} my_buffer_type;

And then reference the struct like a normal type, i.e. get_next_element(my_buffer_type * ptr).

Now my question is: Is there a specific reason for this?

Wikipedia says http://en.wikipedia.org/wiki/Typedef#Usage_concerns

Some people are opposed to the extensive use of typedefs. Most arguments center on the idea that typedefs simply hide the actual data type of a variable. For example, Greg Kroah-Hartman, a Linux kernel hacker and documenter, discourages their use for anything except function prototype declarations. He argues that this practice not only unnecessarily obfuscates code, it can also cause programmers to accidentally misuse large structures thinking them to be simple types.[4]

Others argue that the use of typedefs can make code easier to maintain. K&R states that there are two reasons for using a typedef. First, it provides a means to make a program more portable. Instead of having to change a type everywhere it appears throughout the program's source files, only a single typedef statement needs to be changed. Second, a typedef can make a complex declaration easier to understand.

I personally wonder if there is not enough benefit of having the separate struct namespace to sometimes not use typedef'd structs and since there are several C programming cultures around (Windows C programming has different traditions than Linux C programming in my experience) if there are other traditions that I am not aware of.

Then I am interested in historical considerations (predecessors, first versions of C).

share|improve this question
4  
The purpose of a typedef is to hide the actual type of a variable. Take time_t as an example: if people go around using the underlying integer type, a change in implementation such was the one that will be required in 2038 will break an awful lot of code. If people are using structures without understanding why, that's a failure in the programmer, not the construct. –  Blrfl Apr 23 '13 at 17:45
add comment

2 Answers

up vote 8 down vote accepted

I worked on a large project that extensively used typedef'd structs. We did so for a couple of reasons. Please keep in mind that this was pre-C99 and namespace segregation.

  1. It was easier to type my_buffer_type *buffer instead of struct tag_buffer_type *buffer, and for the size of the codebase (1M+ loc) that made a big difference.

  2. It abstracts the structure into an object. Rather than focus on every individual variable within the struct, we would focus on the data object that it represented. This abstraction led to generally more useful and easier to write code.

    • I think this is in direct contradiction to the quote attributed to Greg Kroah-Hartman that you provided. FWIW, that project was a client / server application and not a kernel so there's definitely some different considerations to take into account.
  3. Treating the structure as an object also enabled us to better encapsulate the information. Code review caught quite a few situations where a newer developer would violate the principles of encapsulation we had put in place. Using typedef'd structs really made those violations obvious.

  4. Portability was a concern as we wrote for a half-dozen platforms with the server and quite a few more with the client.

share|improve this answer
add comment

Every time instead of typing

struct my_buffer_type *buffer;

if you typedef it you can directly use

my_buffer_type *buffer;

typedef's main usege is to reduce the number of keystrokes required to write code and to enhance readability.

share|improve this answer
    
you are right but I have described this fact in my question already. –  wirrbel Apr 23 '13 at 19:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.