Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Regarding assignment to struct Date: Fri, 02 May 2025 13:17:39 -0700 Organization: None to speak of Lines: 76 Message-ID: <87ecx6n4ws.fsf@nosuchdomain.example.com> References: MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Fri, 02 May 2025 22:17:44 +0200 (CEST) Injection-Info: dont-email.me; posting-host="130f159e6ba28748b1540dee603dd110"; logging-data="1976409"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/vNgUJxTLNGjFsggq/qQ3a" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:JgyyIxkW43H6DxyzigyTH8PIUhs= sha1:h8Z7mhXhPwQwbyWzdIMGpnT+Ltg= Xref: csiph.com comp.lang.c:393107 Lew Pitcher writes: > Back in the days of K&R, Kernighan and Ritchie published an addendum > to the "C Reference Manual" titled "Recent Changes to C" (November 1978) > in which they detailed some differences in the C language post "The > C Programming Language". > > The first difference they noted was that > "Structures may be assigned, passed as arguments to functions, and > returned by functions." > > From what I can see of the ISO C standards, the current C language > has kept these these features. However, I don't see many C projects > using them. > > I have a project in which these capabilities might come in handy; has > anyone had experience with assigning to structures, passing them as > arguments to functions, and/or having a function return a structure? C has had structure assignment since before I first learned the language. I may not have been aware of it initially, since it's not mentioned in K&R1, but it's a fundamental feature of the language, and there are almost certainly no current implementations that don't fully support it. I probably don't use structure (or union) assignment very often, but I wouldn't hesitate to use it if called for, particularly for small structures that aren't much bigger than a scalar object. > Would code like > struct ab { > int a; > char *b; > } result, function(void); > > if ((result = function()).a == 10) puts(result.b); > > be understandable, or even legal? I find that particular code is a bit odd. Defining the type "struct ab", an object of that type, and a function returning it all in one declaration is far too terse for my taste, and it's not clear where it would make sense to *define* the function. I don't have any trouble understanding the code, but not because of struct assignment. Returning structs from functions does raise an issue that your code doesn't illustrate. If a struct has a member of array type, and you apply the indexing operator to the array member of a function result, you're accessing an array *object* that doesn't really exist (prior to C11). For example: #include struct foo { int arr[10]; }; struct foo func(void) { struct foo result = { 0 }; return result; } int main(void) { printf("%d\n", func().arr[0]); } The semantics of the indexing operator require it to refer to an array *object*, but as of C99 func().arr is an array *value*, not an object (the expression is not an lvalue). C11 added the concept of *temporary lifetime* to deal with this. It's one of the few cases where you can have a non-lvalue expression of array type. See N1570 6.2.4p8. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */