3 ms·
Unfortunately, the * in the *pointer_my_var syntax is pulling double duty here as it is used both to dereference a pointer as well as declare a pointer. This i
by msla 4y ago
Unfortunately, the * in the *pointer_my_var syntax is pulling double duty here as it is used both to dereference a pointer as well as declare a pointer.
This isn't unfortunate: It's a rule in C that declaration should mirror use, that a variable's declaration should look like that variable's use. This is more obvious when declaring function pointers:
int (*cmp)(char *, char *);
which declares cmp as a pointer to a function which takes two pointers-to-char and returns an int, and obviously cmp is used like:
res = cmp(foo, bar);
What's unfortunate is how it's impossible to escape asterisks in normal text around here.
Also: Don't cast the return values of malloc and friends. It can mask a failure to include stdlib.h, which means the compiler considers the functions to return an int, which is half the size of a pointer on most modern systems, so your pointer just got mangled. Yes, implicit int is Officially Bad in the modern standard. How much do you want to bet your compiler is strict enough to catch this?
- greenyoda 4y ago> What's unfortunate is how it's impossible to escape asterisks in normal text around here. You can escape an asterisk in normal text by putting a backslash in front of it: int (*cmp)(char *, char *); Described here: https://news.ycombinator.com/formatdoc https://news.ycombinator.com/formatdoc (which is linked from the HN FAQ)
- msla 4y agoThat's new. HN didn't have that for the longest time.