If you're writing C code and you are hiding pointer types with typedef, there is a special hell waiting for you.
I'm looking at you libssh with "typedef struct ssh_key_struct* ssh_key". It's already bad enough to hide "struct".
If you're writing C code and you are hiding pointer types with typedef, there is a special hell waiting for you.
I'm looking at you libssh with "typedef struct ssh_key_struct* ssh_key". It's already bad enough to hide "struct".
@galdor I never do that myself, but if the type is meant to be an opaque thing, then I don't really see the problem with it?
@galdor A const ssh_key * is equivalent to struct ssh_key_struct **. In both cases it's a pointer to a pointer to a struct.
The way to think about it is that the "pointer to ssh struct" is the object itself. The fact that it's a pointer is irrelevant. When you declare a ssh_key * all you do is create a pointer to this object type (which happens to be a pointer).
@loke It makes it hard to reason on what's happening.
For example in libssh, some functions take a "const ssh_key *". So now you retain the explicit pointer type in some cases but not others.
Especially annoying for people binding the library.
@loke I'm well aware of what a pointer is.
By masking the pointer type in some cases but not others, you are making your code inconsistent.
You also hide useful information: if a function accepts a parameter of type ssh_key, it is reasonable to wonder if it is a structure passed by copy, a pointer to something you must allocate yourself, or something more complicated altogether.
One good thing with C is that what's happening is usually obvious. Hiding types because you cannot be bothered to write "struct ssh_key *" is a terrible idea.
You may think it's ok because you know that "ssh_key" is a pointer type. But when you have worked with very large C codebases, you learn than you cannot expect to remember what is behind each typedef and must keep things explicit.
Chirp! is a social network. It runs on GNU social, version 2.0.1-beta0, available under the GNU Affero General Public License.
All Chirp! content and data are available under the Creative Commons Attribution 3.0 license.