Matt Joiner
Matt Joiner

Reputation: 118600

Passing pointers to private structs in C?

How can I pass around pointers to structs which have private definitions, without prepending the pointer types with struct?

For example this works:

typedef struct Handle {
    Ino ino;
} Handle;

bool handle_open(Handle **);

But if I move the struct definition into a source file, other source files are forced to use struct Handle *, instead of Handle *.

Upvotes: 1

Views: 353

Answers (3)

Karl Knechtel
Karl Knechtel

Reputation: 61616

if I move the struct definition into a source file, other source files are forced to use struct Handle *, instead of Handle *

Then typedef the pointer, instead of (or in addition to) the struct.

Upvotes: 0

JSBձոգչ
JSBձոգչ

Reputation: 41378

This should go fine in a header:

struct _Handle;
typedef struct _Handle Handle;

Then you can put the actual definition of _Handle in the body of the file that actually manipulates the struct.

Upvotes: 2

lijie
lijie

Reputation: 4871

You can typedef struct Handle Handle;. In this case, Handle is an incomplete type (just like struct Handle).

Upvotes: 5

Related Questions