B
Ben Bacarisse
Johan Bengtsson said:Umm, If your function is used the program will most surely crash for
those cases where it would make any difference to have the function
compared to not have it, it is pointless but the macro isn't.
Ah, then if you are right I have misunderstood. Please give me an
example because the function I suggested did not, I think, "crash" if
used in the context you originally posted (s=spt(b)->s1. Of course,
I often make mistakes, but I've looked again and I don't see what you
mean.
Oh, I do definitely not agree with you there, that's fine however - in
this case it is a genuine disagreement and can be left as one.
OK!
Agreed, there is however also a point in making it readable and it
those two goals are in conflict - what to pick? Well that is a thing
to have as much disagreement as wanted.
a=spt(*pp)->c;
would mean exactly the same as
v=*pp;
a=spt(v)->c;
Yes, that is what I meant. I should have written "mean (roughly) the
same as '(v = *pp), (spt(v)->c)'. In other words I was extolling the
virtue of what is often called referential transparency in
expressions. You've said above that you don't value it, so I will not
push the case any more.
Did you intend this: spt(*pp++)->c;
Because then I do agree that it is dangerous and your point is valid,
your solution, however, isn't.
That is not what I meant, but I *could* have written it because it
gives a good example of why a function is so often a safer solution.
spt(*pp++)->c is dangerous when spt is the macro you posted but is
safer (in that it has fewer surprises in what it means) if it is the
function I gave. All this, of course, subject to your first point -- I
can't see the error to which you refer.
[BTW, this thread having gone on longer than I'd expected, I've had
time to reflect that I should have made spt return a "const struct A
*" -- such functions are safer still if they object they return a pointer
to is read-only.]