| Deutsch English Français Italiano |
|
<vv4olm$388j7$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: Regarding assignment to struct
Date: Sat, 3 May 2025 11:46:30 +0200
Organization: A noiseless patient Spider
Lines: 68
Message-ID: <vv4olm$388j7$1@dont-email.me>
References: <vv338b$16oam$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 03 May 2025 11:46:32 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7b78af53c70b365d539b522384cbe99d";
logging-data="3416679"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/jVujfzQ0y7p7/Sgg6f7VmtPfplFkIiZ8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:WmdxK9PlV37sXQv1XNBR35d0ugU=
In-Reply-To: <vv338b$16oam$1@dont-email.me>
Content-Language: en-GB
On 02/05/2025 20:34, Lew Pitcher wrote:
> 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 use these features regularly. I have no problem passing structs
around if that is the convenient way to structure the code.
Some people mistakenly think that it is very inefficient, in comparison
to passing around pointers to structs (which is the usual alternative).
There are circumstances where you might end up with an extra struct and
an extra copy, but unless you are dealing with very big structs and
otherwise very fast functions, it's unlikely to be significant. Modern
ABI's support passing small structs around in registers, and bigger
structs get passed around using hidden pointers - using the structs in
your code, rather than pointers to structs, makes the code clearer,
safer, and gives the optimiser more information for better static
analysis and code generation.
>
> 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?
>
> 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'd immediately reject any code that mixes declaration of a variable and
a function in the same declaration. I'd immediately reject any code
that defines a type and declares a function in one shot. I'd question
code that defines a type and a variable in one go. But that's my way of
coding - other people have different rules, and your declarations are legal.
Personally, I'd have :
typedef struct {
int a;
char * b;
} ab;
ab result;
ab function(void);
(Obviously "ab" would not be a likely name in real code.)
Once the type "ab" is defined, I am quite happy making variables of it,
assigning them, and using it for function parameters and return types.