Sujet : Re: Regarding assignment to struct
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 02. May 2025, 21:17:39
Autres entêtes
Organisation : None to speak of
Message-ID : <87ecx6n4ws.fsf@nosuchdomain.example.com>
References : 1
User-Agent : Gnus/5.13 (Gnus v5.13)
Lew Pitcher <
lew.pitcher@digitalfreehold.ca> 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 <stdio.h>
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.comvoid Void(void) { Void(); } /* The recursive call of the void */