Sujet : Re: That depends... (Was: Regarding assignment to struct)
De : janis_papanagnou+ng (at) *nospam* hotmail.com (Janis Papanagnou)
Groupes : comp.lang.cDate : 03. May 2025, 05:17:51
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vv45dg$2og84$1@dont-email.me>
References : 1 2 3 4
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0
On 03.05.2025 03:13, Lew Pitcher wrote:
[...]
My goal is to add a feature to one of my older programs. The feature will
require passing a number of objects down and up the function call chain, and
I'm looking at various alternatives to accomplish this.
ATM, it looks like I either go with "global" objects, or I change the
argument list of several functions to include about 5 more objects each.
Because these additional objects all relate to one another, I'm thinking
of grouping them in a struct, and passing a pointer to the struct back
and forth.
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.
When I read about that I immediately thought about 'complex' numbers
(and functions that process structs of two floats). I'd expect that
such code would exist (maybe not for a 'complex' type but some struct).
It would IMO certainly provide a "better" interface to pass 'complex'
items than to pass the pieces of complex numbers or use other kludges.
Where I'd be more reluctant is to pass (as in your original example)
also pointers to items as 'struct' attribute; not that this should be
a problem, but to me that has a smell [in the general case] and I'd
in such cases might just return a pointer to a linked structure. (But
I think that's just a matter of taste of course.) In your case it may
make perfectly sense.
If it is legal, then why isn't it used more often?
Reasons I can think of are; compatibility considerations (to really
old K&R "C" versions), support of compilers (whose development was
abandoned but still in use because they "just work"), maybe just not
knowing that feature, or lacking the information, or bad paragons,
or having got (bad or good?) advice.
I seem to recall my student days where we learned (based on old K&R)
that you cannot return or assign structs in "C" (as opposed to e.g.
Pascal that has a powerful ':=' assignment that allowed to assign
complex nested data structures). Such [outdated] information might
have contributed to only rare uses of structs in those contexts.
Is it a readability/
maintainability issue, or is it something else?
I don't see how that could be the case. Sure, you can write arbitrary
badly readable or badly maintainable code. But, as I'd see it e.g. for
structs like the above mentioned 'complex' type functions it would IMO
add to its legibility.
If that makes the organization and handling of your functions simpler
or better organized (and don't have any legacy requirements) then I'd
just use the feature.
Janis