Sujet : Re: That depends... (Was: Regarding assignment to struct)
De : jameskuyper (at) *nospam* alumni.caltech.edu (James Kuyper)
Groupes : comp.lang.cDate : 03. May 2025, 05:47:06
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vv474a$2pg8e$1@dont-email.me>
References : 1 2 3 4
User-Agent : Mozilla Thunderbird
On 5/2/25 21:13, Lew Pitcher wrote:
...
But... I remembered this (apparently) seldom-used feature of being able
to pass a struct by value up and down the chain (down, as a function
argument, up as a function return value). As I've not done this before,
and I've not /seen/ it done in other peoples code, I wonder how viable
a solution it might be.
If it is legal, then why isn't it used more often? Is it a readability/
maintainability issue, or is it something else?
The most basic reason is that a pointer to a struct is smaller than most
structs (particularly one that contains 5 items). In addition, passing
it up and down the chain nominally requires making multiple copies of
it, one in each function that takes it as an argument, and another copy
when you store the return value - though compilers can optimize some of
that away. Changes made to one copy will not be made to the other
copies, which is very often not what you want.
The cases where I've seen the approach used the most successfully were
to implement things like complex numbers (before they were added to C99).