简体   繁体   中英

Redeclaring opaque struct in C file

I'm working on a header file that declares some opaque struct s which is supposed to be defined in the corresponding C file. Here it is:

decl.h

#ifndef DECL_H
#define DECL_H

typedef struct test_t test;

#endif //

Some library that is supposed to be used in the implementation defines another opaque struct in its header lib.h :

//...
typedef struct _library_struct_t library_struct;
//...

Now in my decl.c file I want to make struct test_t to be the same (or compatible) with the library_struct . I tried this:

decl.c

//...
typedef library_struct test; //error: conflicting types for ‘test’
//...

But it does not compile. So the only way to go I can see now is

struct test_t{
    library_struct *lib_struct_ptr;
};

Is there shorter or more convenient way? Both test and library_struct are opaque. Why can't I make the test to be the same as library_struct ? Can macros be helpful here?

your code is equivalent to

typedef struct test_t test; /* from decl.h */
typedef library_struct test; /* in decl.c */

So you redefine test and of course the compiler doesn't accept that

I don't know what you expect to do through macros but redefinition is not allowed.

In the worst case you can hide the type of a pointer with a void * then casting to the type you (hope) have, but this is obviously dangerous because the compiler will follow you at your own risk .

The compiler does not check the types against you but to help you to see your errors at compile time ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM