It is a matter of taste, but I prefer a lot the first style in C: having opaque struct
with explicit and systematic pointer to them. BTW, GTK is systematically following such a style. And so does standard <stdio.h>
with its FILE
.
C is a low level language, and the programmer is expecting to understand low level details. He usually expects to know (or to intuit) the size of a given type. He knows that passing a pointer is really fast. And the programmer should know that it is a pointer. Otherwise, he might be tempted (after several layers of software) to "pass by reference", that it to pass a pointer to, that opaque thing.
In C you can sometimes pass a struct
by value, and in effect all the data is copied when you do that. This is sometimes good, when the struct
is small (e.g. struct Point_st { int x, y;}
for cartesian coordinates...). But in other cases, it is inefficient (for example, you probably don't want to pass an entire struct stat
from stat(2) by value).
At last, on some ABIs (notably x86-64 for Linux) there might be a special calling convention for small struct
. On x86-64 a struct
with two pointer fields is returned thru registers (not using any memory zone for that).
In other languages (Go notably, and probably C++) the situation is different, and the second style is preferable (but C++ has const Opaque&
to pass by "constant reference", in effect "hiding" a pointer).
The asterisks don't make the code any more readable,
That is your opinion, and I disagree with it. They make IMHO the code a lot more readable.
typedef
s; if you do, you have to guarantee that your API hides all aspects of that type's pointer-ness from the consumer.