0

Such as:

typedef struct _cairo_clip cairo_clip_t;

Why not directly use _cairo_clip? See numerous similar definitions in some code.

Samuel Edwin Ward
  • 6,526
  • 3
  • 34
  • 62
user1279988
  • 757
  • 1
  • 11
  • 27
  • If I changed your post off from your initial question, please, edit it back. – Rubens Feb 23 '13 at 02:43
  • http://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions – makes Feb 23 '13 at 02:47
  • FYI on the struct _cairo_clip. The prefix _ would indicate that the author wants to somehow hide or "privatize" the name of the structure. This is poor form IMHO. There is no reason you can't type "struct cairo_clip" when you want to refer to the type of "struct cairo_clip". Most of the time you see this from people coming from other languages to C. They think the extra typing is somehow bad. C is a simple language. Writing struct cairo_clip is not bad form. Other than the abstraction that Mankarse mentions, there isn't much benefit. Also some would say the leading _ and _t are bad. – Josh Petitt Feb 23 '13 at 03:46

2 Answers2

3

The idea behind typedef is to let you skip the struct keyword. Unlike C++, C does not let you do this:

struct _cairo_clip {
    int a;
    float b;
};
_cairo_clip cc;        // Not allowed
struct _cairo_clip cc; // Allowed, but requires a keyword
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

If you tried to directly use _cairo_clip, you would need to call it struct _cairo_clip.

struct _cairo_clip is more verbose than cairo_clip_t.

Using a typedef also provides some abstraction, because it means that cairo_clip_t can be implemented as either a built-in type or as implemented as a struct, without causing a change in the syntax of the client code.

Mankarse
  • 39,818
  • 11
  • 97
  • 141