C
Capstar
Hi NG,
I've read some time ago in this NG, that when you're writing a library
it can be good practice to use an opaque type to point to your
structures if the user doesn't need to modify the structure himself.
This way the internal structure of the lib can be changed, and the lib
can be recompiled, but the application using the lib doesn't need to be
recompiled.
But then there are two ways to define the opque type:
#typedef struct libstruct lib_t;
or
#typedef struct libstruct * lib_t;
I'm not sure which one to use. I also read somewhere that it's bad
practice to make a typedef to a pointer in such a way that it is not
obvious from looking at the type, in this case lib_t, that it is a
pointer to whatever. So that would mean it would be best to use the
first typedef.
But if I use the first way, In the application I need to declare
lib_t *my_opaque_variable;
If I don't do it that way, I will get a compiler error because it
doesn't know the size of lib_t.
So that would mean the second typedef would be better, because the
compiler will always know the sizeof of a pointer.
So what is the usual way to do this?
Thanks in advance,
Mark
I've read some time ago in this NG, that when you're writing a library
it can be good practice to use an opaque type to point to your
structures if the user doesn't need to modify the structure himself.
This way the internal structure of the lib can be changed, and the lib
can be recompiled, but the application using the lib doesn't need to be
recompiled.
But then there are two ways to define the opque type:
#typedef struct libstruct lib_t;
or
#typedef struct libstruct * lib_t;
I'm not sure which one to use. I also read somewhere that it's bad
practice to make a typedef to a pointer in such a way that it is not
obvious from looking at the type, in this case lib_t, that it is a
pointer to whatever. So that would mean it would be best to use the
first typedef.
But if I use the first way, In the application I need to declare
lib_t *my_opaque_variable;
If I don't do it that way, I will get a compiler error because it
doesn't know the size of lib_t.
So that would mean the second typedef would be better, because the
compiler will always know the sizeof of a pointer.
So what is the usual way to do this?
Thanks in advance,
Mark