Sujet : Re: technology discussion → does the world need a "new" C ?
De : bc (at) *nospam* freeuk.com (Bart)
Groupes : comp.lang.cDate : 15. Aug 2024, 23:22:39
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v9lv3e$1465l$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
User-Agent : Mozilla Thunderbird
On 15/08/2024 22:36, Tim Rentsch wrote:
Bart <bc@freeuk.com> writes:
On 15/08/2024 09:43, Tim Rentsch wrote:
>
Bart <bc@freeuk.com> writes:
>
[on comparing array arguments in C with call-by-reference]
>
[...] the differences [between C rules and true call-by-reference]
can be summarised here; [...]
>
Call Array access in callee
>
C call-by-value F(A) A[i]
>
true call-by-reference H(A) A[i]
>
What the user has to write is what's important, and here it is clear
that they write the same thing [in the two cases shown].
>
The comparison above is misleading because it is incomplete.
Let's compare the two modes more fully:
>
>
C call-by-value call-by-reference
=============== =================
at call:
>
(array argument) F(A) H(A)
>
(pointer argument) F(p) (disallowed)
>
My posts were about passing *arrays* and the fact that C's
pass-by-value was remarkably similar to pass-by-reference.
I see. So your point is, if we ignore all the ways that the two
modes are different then they are exactly the same.
Brilliant deduction, Dr. Watson.
So your approach is to totally ignore all the ways that the two modes are identical. OK. (Maybe we can also say that arrays and pointers are entirely separate concepts in C, because they are different in /some/ ways!)
Suppose we write functions that sum an array's elements then zeros the elements in the caller's array at the same time (to give the reference element some observable behaviour in the caller). We do three versions F, G and H:
F uses C's call-by-value. The characteristics here are:
Param syntax: int A[]
Access in func: A[i]
Call syntax: F(A)
G emulates call-by-reference. The characteristics are:
Param syntax: int (*A)[]
Access in func: (*A)[i]
Call syntax: F(&A)
H uses a fantasy version of C with proper pass-by-reference. Those characterics are now:
Param syntax: int &A[] (in this fantasty, no () is needed)
Access in func: A[i]
Call syntax: F(A)
Eagle-eyed readers may notice a remarkable similarity between F and H. The only point of difference is that lone &, written once in the program (compared with perhaps dozens of calls and accesses).
But according to you, that is pure coincidence; nothing to see, move on!
To me however it is highly significant. Most of the downsides of G, namely the ugly syntax, are done away with F. And yet, while F doesn't use call-by-reference in any way, it still can magically update the caller's array even though it uses only call-by-value.
I'd say that F confers most of the benefits of pass-by-reference, but you presumably consider F to provide 0% of the benefits? (What extra benefits could H provide in my scenario.)