Search code examples
cstructtypedeflibgphoto2

Why define a _struct and then typedef it without _?


I was browsing the libgphoto2 repositories and saw this:

struct _PTPObjectHandles {
    uint32_t n;
    uint32_t *Handler;
};
typedef struct _PTPObjectHandles PTPObjectHandles;

Why would somebody want do that instead of just calling the struct PTPObjectHandles and omitting the whole typedef line?

EDIT: I should probably note that _PTPObjectHandles is never again used in the code.


Solution

  • The most common reason for doing this is simply to make it shorter to type and begin to look like a "real data type".

    EG, this:

    struct _PTPObjectHandles *ptr1;
    int something;
    struct _PTPObjectHandles *ptr2;
    

    Simply looks "cooler" if you rewrite it as:

    PTPObjectHandles *ptr1;
    int something;
    PTPObjectHandles *ptr2;
    

    That's the main reason.

    However, it also provides the library developer that is making you use it the ability to re-typedef it as something else in the future. I've seen cases (eg, OpenSSL) that changed a typedef from a real struct to a pointer to a struct (for example). (Granted, that didn't work, but you can imagine the case where a typedef does actually change and it would work.)

    So, do you have to? No.

    Do people do it to try and make code more readable? Yes.

    Note that a C++ class is actually doing about the same thing. If you go read a good C++ from the ground up kind of book, you'll find it first starting with a struct and then changing the 'struct' word to 'class' and starting to instantiate it using the straight name.

    Edit: And... make sure you read the comment from @Bathsheba below and check proper naming conventions within C. As he points out, it should be _pTPObjectHandles with a lower-case p. For my own style, I avoid using uppercase typedefs too simply because I like my upper-case types to be true classes in C++ and want to know when they're just typedefs. But, again, that's a style choice.