News for hackers. Stuff that matters.
Re:What is your favorite C programming trick? (2009) by jhallenworld on Sunday November 22, 2020 @09:50AM (#25177840)
Use arrays of 1 to avoid using '.' and '&'. So if you have a structure, say:

    struct foo { int a, b; };
A function that works on the structure looks like this, you use '->':

    void pr(struct foo *n) { printf("%d %d\n", n->a, n->b); }
But when you make an instance of this structure, you have to use this different syntax:

    struct foo p;
    p.a = 1;  // Yuck!
    pr(&p); // Bad!
But you can do this instead:

    struct foo p[1];
    p->a = 1; // Very nice..
    pr(p); // Yes!
Re:What is your favorite C programming trick? (2009) by ptr on Sunday November 22, 2020 @10:08AM (#25177977)
Curious, why don’t you like ‘.’ and ‘&’?
Re:What is your favorite C programming trick? (2009) by jhallenworld on Sunday November 22, 2020 @10:11AM (#25177990)
Why does C even have two member selection operators?

Anyway it means you can't cut and paste code from one place to another without changing '.' to '->' or vice-versa.

Re:What is your favorite C programming trick? (2009) by flohofwoe on Sunday November 22, 2020 @10:41AM (#25178297)
A '->' is always a runtime indirection involving an extra memory access, while a '.' is always resolved into a single offset at compile time.

E.g. a:

int x = a->b->c->d;

means there's 3 memory accesses, while

int x = a.b.c.d;

means there's one memory access for the whole expression.

Also consider this:

int x = a->b.c->d;

I can immediately see where pointer indirections are happening.

...unless you're in C++ of course which messed up this simple rule when references were added to the language.

Re:What is your favorite C programming trick? (2009) by jhallenworld on Sunday November 22, 2020 @11:02AM (#25178494)
>A '->' is always a runtime indirection

It's not true if the compiler can figure out that the left side is a constant, consider:

    struct foo { int a; };
    struct foo z = { 7 };
    struct foo *const p = &z;
Then in z->a, no indirection is necessary. GCC -O2 makes this:

    int fred() { return p->a; }

    fred:
        movl    z(%rip), %eax
        ret

    p:
        .quad   z

    z:
        .long   7
Re:What is your favorite C programming trick? (2009) by flohofwoe on Sunday November 22, 2020 @11:11AM (#25178572)
That's (usually) only true for the very first '->' in a chain and as you said, depends on the compiler figuring out if the pointer indirection can be resolved at compile time.

A chain of '.' on the other hand is always guaranteed to be resolved into a single offset at compile time.

Re:What is your favorite C programming trick? (2009) by ben-schaaf on Sunday November 22, 2020 @10:20AM (#25178071)
> Why does C even have a two member selection operators?

Because using `(*ptr).member` everywhere is annoying. There's plenty of times you want or need to have direct access to a member rather than always dereferencing a pointer.

Re:What is your favorite C programming trick? (2009) by abecedarius on Sunday November 22, 2020 @01:14PM (#25179552)
It's too bad C's pointer-deref operator is prefix instead of postfix. In Pascal it's ^ so you write ptr^.member and there's no special -> operator. Even better, declarations and expressions would read intuitively left-to-right instead of spiraling out through stars on the left and brackets on the right.
Re:What is your favorite C programming trick? (2009) by btrask on Sunday November 22, 2020 @02:03PM (#25180025)
In C you can use [0] for postfix pointer dereferencing.
Re:What is your favorite C programming trick? (2009) by abecedarius on Sunday November 22, 2020 @04:03PM (#25181079)
Alas, that's clumsy, and for declarations not possible. I have used it in expressions at times.

Here's a variation that seems plausible: make postfix p^ be like C's p[0], and infix p^i like C's p[i]. (With a tighter binding for ^ than C has.)

Re:What is your favorite C programming trick? (2009) by Twisol on Sunday November 22, 2020 @03:42PM (#25180871)
So that's where the lens notation comes from...

https://github.com/ekmett/lens/wiki/Examples

> ("hello","world")^._2

Re:What is your favorite C programming trick? (2009) by jhallenworld on Sunday November 22, 2020 @10:23AM (#25178105)
ptr.member should work here- I mean the compiler knows the left side is a pointer, so it should automatically dereference it.
Re:What is your favorite C programming trick? (2009) by beagle3 on Sunday November 22, 2020 @12:55PM (#25179414)
C was practically a portable assembler when it was designed, and it was likely helpful for performance reasoning that all indirections were clearly visible.