Sujet : Re: technology discussion → does the world need a "new" C ?
De : janis_papanagnou+ng (at) *nospam* hotmail.com (Janis Papanagnou)
Groupes : comp.lang.cDate : 10. Jul 2024, 16:09:56
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v6m886$1v629$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 23
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0
On 10.07.2024 16:49, bart wrote:
On 10/07/2024 14:32, Ben Bacarisse wrote:
>
Except when it comes to arrays.
>
The oddity is that, in C, one can't pass arrays to functions at all.
That is one of the quirks that people learning C need to learn. It does
not alter the fact that there is only parameter passing mechanism -- by
value.
>
Your plan, of course, is to take that one place where C is relatively
simple
It is not that simple. It is confusing. It is error prone.
I earlier asked this:
"So if arrays aren't passed by value in C, and they aren't passed by
reference, then how the hell ARE they passed?!"
Arrays are not passed to the function; the address if the first array
element is passed as a pointer value to the function.
I haven't had a reply yet. I still consider arrays in C to be 'passed'
by a mechanism which is near-indistinguishable from actual
pass-by-reference.
It can be distinguished. Call by reference means that you provide an
object to the function that can be changed in its environment. In C,
where a pointer to an array is passed, you can (for example) also
pass a 0-pointer value.
If somebody had proposed adding pass-by-reference for arrays, you'd say
C doesn't need it, because whatever benefits it might have you, C
already has!
This is obviously wrong. - You can (to some degree) emulate a reference
mechanism by using pointer values. But "C" doesn't have a mechanism to
pass "by reference". (Note that "pass by reference" is not a colloquial
term like we may colloquially use "reference". It's rather a technical
term with a specific - and I hope quite unique :-) - meaning.)
(For arrays it is (conveniently?) hidden by the language as designed,
but you can still observe it by behavioral side effects.)
There's also another advantage of references; try implementing such
simple things like binary tree operations (including updates) once
with references (in C++) and once with pointers emulating references.
(Or read some basic book about Algorithms and Data Structures where
you typically find such code and can simply study it.)
Janis