c typedef(ed) opaque pointer
Asked Answered
S

1

21

I've defined an opaque structure and related APIs like this:

typedef struct foo foo;
foo *create_foo(...);
delete_foo(foo *f);

I am not able to define the structure in my c file. Gives redefinition error.

typedef struct foo {
   int implementation;
}foo;

I am able to use foo in c file without typedef but I want the typedef (i.e. use it directly as foo*). Is there a way?

Snowshoe answered 14/3, 2011 at 13:34 Comment(0)
S
28

You already have the typedef in your header, so include that and define struct foo in the implementation without the typedef.

foo.h:

typedef struct foo foo;
foo *create_foo(...);
delete_foo(foo *f);

foo.c:

#include <foo.h>

struct foo { int implementation; };
/* etc. */
Sublittoral answered 14/3, 2011 at 13:37 Comment(1)
True. I can just use it in c file without typedefing it again. Dumb of me to ask such question. Thanks for making me realize that. :)Snowshoe

© 2022 - 2024 — McMap. All rights reserved.