3 ms·
Hey array indexing is postfix, so let's just use the old "arrays decay to pointers" staple, but obviously in reverse: typedef struct { int a;
by unwind 3y ago
Hey array indexing is postfix, so let's just use the old "arrays decay to pointers" staple, but obviously in reverse:
typedef struct {
int a;
int b;
} foo;
int main(void) {
foo one = { 1, 2 };
foo * const p = &one;
p[0].b = 13; // Look ma no -> !
printf("Now we have { %d, %d }\n", one.a, one.b);
return 0;
}
Of course one could go all nasty and flip the base and index:
0[p].b = 13;
But hey that's just being silly.
In all seriousness, thanks a lot for the historic review of C syntax! Very interesting, as someone who really likes C enough to (ab)use it whenever possible.
I know that a->b is equivalent to (*a).b, but still the difference makes sense to my brain which first learned assembly. It's much "lighter-feeling", not having to evaluate the "entire" structure just to get to a single field. :) Weird, but I really like this little piece of syntactic sugar. Yum.