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.
By masking the pointer type in some cases but not others, you are making your code inconsistent.
This one I disagree with. Unless they also expose ssh_key_struct of course. Could it be that you made a typo in your previous message and when you wrote: const ssh_key * you means to say const ssh_key_struct *?
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.
This is much more valid argument in my opinion, and the main reason I never hide pointers behind typedefs. And if it's done, then the underlying struct that the typedef'ed pointer points to should be internal and not visible in the main header.
GNU social JP is a social network, courtesy of GNU social JP管理人. It runs on GNU social, version 2.0.2-dev, available under the GNU Affero General Public License.
All GNU social JP content and data are available under the Creative Commons Attribution 3.0 license.