Advertisement Jump to content
Sign in to follow this  

Is there a Pimpl idiom for C?

This topic is 3848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I see that there's one for C++. I've done some Googling but haven't seen any for C. If someone has an example or link that would be great.

Share this post

Link to post
Share on other sites
The way to do it in C is to declare an opaque struct in the header file and hide the implementation in the C file. This has two effects:

1. The definition of the structure is hidden in the C file so only that file needs to be recompiled if the structure changes. This is what the pimpl idiom is good for.

2. The variables has been made private so the data is encapsulated.

Many C API's does this. For example Lua.

// Gadget.h

// Declare the struct to allow client code to keep pointers to a Gadget.
typedef struct Gadget Gadget;

Gadget* GadgetCreate();
void GadgetDestroy(Gadget*);
int GadgetDoStuff(Gadget*);

// Gadget.c

// Define the struct
struct Gadget

// Implement the functions

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!