Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Learning functions; roots of the quadratic; two return values Date: Sun, 04 Oct 2015 12:56:49 -0700 Organization: None to speak of Lines: 35 Message-ID: References: <87eghbpzr2.fsf@bsb.me.uk> <56102ca0$0$3053$426a74cc@news.free.fr> <8737xrpwf0.fsf@bsb.me.uk> <87oagfobcl.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="31247"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19+uIOZc91PIaPx/6ejylvL" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:lB31xsqehgdNZYFEPa27gegO3eo= sha1:ASCYKXaC8ebwOM6om7K+hPLnQn8= Xref: csiph.com comp.lang.c:71843 Ben Bacarisse writes: [...] > I tried this with lcc but the code did not compile. Cutting it down to > a minimal example, the problem seems to be with compound literals that > include an array: > > typedef struct s { double r[2]; }; > > struct s f1(double a) > { > return (struct s){ { a, 0 } }; > } Interesting example. Is the compound literal necessary to trigger the bug? (BTW the "typedef" keyword can be omitted, since you didn't give it a name.) Returning a structure that contains an array creates an object with *temporary lifetime*, a concept that wasn't introduced until C11. In C99 and earlier, something like f1(1.5).r[0] has undefined, or at least vaguely defined, behavior. Array indexing requires an array object, but prior to c11 there is no array object to be indexed, just an array value that's part of the returned struct value. [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"