• Re: Regarding assignment to struct

    From Tim Rentsch@tr.17687@z991.linuxsc.com to comp.lang.c on Sun Mar 1 18:54:36 2026
    From Newsgroup: comp.lang.c

    Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:

    Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

    Andrey Tarasevich <noone@noone.net> writes:
    [...]

    #include <stdio.h>

    struct S { int a[10]; };

    int main()
    {
    struct S a, b = { 0 };
    int *pa, *pb, *pc;

    pa = &a.a[5],
    pb = &b.a[5],
    pc = &(a = b).a[5],
    printf("%p %p %p\n", (void *) pa, (void *) pb, (void *) pc);
    }

    This version has no UB.

    I believe it does. [...]

    If you look again carefully, I expect you will reach a
    different conclusion.

    Please explain. Or don't.

    I explained, in text that you snipped, why I think it has UB.
    If there's a flaw in my reasoning, I invite you to point it out
    without being mysterious.

    I wasn't thinking about your reasoning or what flaw there may have
    been in it. My intention was to suggest a course of action that
    would be helpful rather than to point out any sort of mistake. But
    since you ask, I think the mistake you made was jumping to a
    conclusion without looking carefully enough at the problem.
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Keith Thompson@Keith.S.Thompson+u@gmail.com to comp.lang.c on Sun Mar 1 19:36:03 2026
    From Newsgroup: comp.lang.c

    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
    Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
    Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
    Andrey Tarasevich <noone@noone.net> writes:
    [...]

    #include <stdio.h>

    struct S { int a[10]; };

    int main()
    {
    struct S a, b = { 0 };
    int *pa, *pb, *pc;

    pa = &a.a[5],
    pb = &b.a[5],
    pc = &(a = b).a[5],
    printf("%p %p %p\n", (void *) pa, (void *) pb, (void *) pc);
    }

    This version has no UB.

    I believe it does. [...]

    If you look again carefully, I expect you will reach a
    different conclusion.

    Please explain. Or don't.

    I explained, in text that you snipped, why I think it has UB.
    If there's a flaw in my reasoning, I invite you to point it out
    without being mysterious.

    I wasn't thinking about your reasoning or what flaw there may have
    been in it. My intention was to suggest a course of action that
    would be helpful rather than to point out any sort of mistake. But
    since you ask, I think the mistake you made was jumping to a
    conclusion without looking carefully enough at the problem.

    The mistake I made was failing to notice that the line with the
    assignment to pc ends with a comma operator rather than a semicolon.

    The code was deliberately obfuscated, and I fell for it.

    I acknowledged that nearly a year ago. Why bring it up now?
    I find it extremely annoying.
    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    void Void(void) { Void(); } /* The recursive call of the void */
    --- Synchronet 3.21d-Linux NewsLink 1.2