Sujet : Re: technology discussion → does the world need a "new" C ?
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 10. Jul 2024, 02:18:02
Autres entêtes
Organisation : None to speak of
Message-ID : <87le2avyd1.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
User-Agent : Gnus/5.13 (Gnus v5.13)
bart <
bc@freeuk.com> writes:
On 10/07/2024 00:50, Keith Thompson wrote:
bart <bc@freeuk.com> writes:
[...]
Arrays can be passed by explicit reference:
>
void F(int(*A)[20]) {
printf("%zu\n", sizeof(*A)/sizeof((*A)[0])); // shows 20
}
>
That can be called like this:
>
int a[20];
F(&a);
On the language level, that's passing a pointer to an array object.
The pointer itself is passed by value. Passing a pointer to an array
is conceptually no different than passing a pointer to anything else.
>
I was replying to:
>
"In C, arrays are not passed to functions, period."
Which is a correct statement.
[...]
But notice how C gives exactly the same result as my code that used
by-reference, even though:
>
* C "doesn't pass arrays by reference"
* C's F function uses the same parameter type (only & is missing; maybe
by-ref is implicit...)
* No explicit de-ref is needed inside F
* No explicit address-of is needed when calling F
Right. The C rules that make all this possible have been explained
to you many times. I won't waste my time explaining them to you
again. If you were interested in learning, you would read section
6 of the comp.lang.c FAQ.
Yes, some of C's rules make it *look like* arrays are passed by
reference.
So C behaves exactly as though it passes arrays by-reference, and yet
it doesn't have pass-by-reference. In fact, C does it without even
needing to be told!
If you actually believed that C has pass-by-reference for arrays, it
would indicate that you don't understand C. But you're only pretending
to believe it.
If C had pass-by-reference for arrays, then presumably you could obtain
the size of an array parameter by applying sizeof to its name, and you
could get the address of an array parameter by applying unary "&" to its
name. I know why that doesn't work. And so do you.
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */