Liste des Groupes | Revenir à cl c |
bart <bc@freeuk.com> writes:That's what my example did. But only if the array has a specific bound in the parameter type, not if it's unbounded, since (1) a function can be passed arrays of different sizes; (2) C arrays don't normally contain their length.On 10/07/2024 00:50, Keith Thompson wrote:Which is a correct statement.bart <bc@freeuk.com> writes:>
[...]Arrays can be passed by explicit reference:On the language level, that's passing a pointer to an array object.
>
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);
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."
[...]
But notice how C gives exactly the same result as my code that usedRight. The C rules that make all this possible have been explained
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
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 yetIf you actually believed that C has pass-by-reference for arrays, it
it doesn't have pass-by-reference. In fact, C does it without even
needing to be told!
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 youThe by-ref in my language has a "&" has part of the parameter type; which would be cancelled by the "&" in "&a", so it would print the value of the passed pointer - the address of the array in the caller.
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.
Les messages affichés proviennent d'usenet.