5 ms·
Anytime I write C code, I try to keep the asterisk close to the type: void* foo(void* bar); I know that the asterisk technically belongs with the variable nam
by 4lch3m1st 9y ago
Anytime I write C code, I try to keep the asterisk close to the type:
void* foo(void* bar);
I know that the asterisk technically belongs with the variable name when declaring it, but it helps to wrap my head around them, as a reminder that they're a pointer type.
- sigjuice 9y agoThe world mostly does it the other way. A couple of code examples: https://github.com/apache/httpd/blob/trunk/server/provider.c https://github.com/apache/httpd/blob/trunk/server/provider.c https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/kernel/fork.c https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.... Almost all documentation, references and literature: Here are a couple of man pages. http://man7.org/linux/man-pages/man3/gethostbyname.3.html http://man7.org/linux/man-pages/man3/gethostbyname.3.html https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/execv.3.html https://developer.apple.com/library/mac/documentation/Darwin... Here is a draft of the ISO C standard: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf
- ngcc_hk 9y agoI agree you based on fact that more in code we use void *s = then use it as s= But I agree more that the void* s = then use it as s= Is more understandable as the first set confuse me when I learn pointer. It still does. void* is a type like statement. And void is like a return type statement.
- millstone 9y agoThis totally destroyed me when I was learning C. The theory was: int *foo; makes more sense, because to access the int, you write: *foo = ...; But that's not an int, it's a crash! If you want an int, you have to make one. Placing the asterisk next to the type made it all click for me.
- Y_Y 9y agodouble foo; compute(foo); That's not even a crash, just silent garbage. I don't know how you expect to do better with an uninitialised variable (except ints that default to 0). foo dereferences to an int, but what could you hope to happen if you never gave it some memory to point to?
- jcelerier 9y agoThe only thing that sucks hard is when you do : int* s1, s2; here s2 is an int, not an int* and you have to do int* s1, *s2; this is braindead imho
- megaman22 9y agoAs long as you're not using prehistoric versions if C, you don't have to define all variables up front, so there is much less justifiable reasons for that kind of multiple declaration.
- Koshkin 9y agoExcept nothing will save you from the "one true way to write C" when you try to write a declaration of a pointer to a function, for example.
- jcelerier 9y agoyep, I prefer the c++ way by far : using foo = void(int, float);
- sigjuice 9y agoYou can adjust your function pointer typedefs if you like. e.g. signal(2) http://man7.org/linux/man-pages/man2/signal.2.html http://man7.org/linux/man-pages/man2/signal.2.html typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler); can be rewritten as typedef void sighandler_t(int); sighandler_t *signal(int signum, sighandler_t *handler);