Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Regarding assignment to struct Date: Mon, 05 May 2025 06:34:49 -0700 Organization: A noiseless patient Spider Lines: 43 Message-ID: <868qnb5gg6.fsf@linuxsc.com> References: <86plgo7ahu.fsf@linuxsc.com> <87o6w7h2wn.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Mon, 05 May 2025 15:34:52 +0200 (CEST) Injection-Info: dont-email.me; posting-host="b2f20e442295d938e3644788cf940be7"; logging-data="629572"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+LSUUpE3kd+yib2SlAw/muZR165rW2d0I=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:gq4pRF2C+IwIIAdmD/hp8cv0X9U= sha1:2qNixOlAXQrGmyq9c2oxpmU7enc= Keith Thompson writes: > Andrey Tarasevich writes: > [...] > >> #include >> >> 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", pa, pb, pc); >> } > > [...] > > I think that code has undefined behavior. Right. [*] > (a = b) is an rvalue that refers to an object of type struct S with > temporary lifetime. pc holds the address of a subobject of that > temporary object. The object reaches the end of its lifetime at the end > of the evaluation of the full expression. You then print its value. Even if the printf() statement were replaced by (void)pc; the behavior would be undefined, because the pointer held in pc becomes indeterminate as soon as the statement containing the assignment to pc completes. [*] Assuming C11 semantics. At best inadvisable under C99 semantics, and a constraint violation under C90 semantics.