Sujet : Re: technology discussion → does the world need a "new" C ?
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 09. Jul 2024, 23:32:45
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240709152805.587@kylheku.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-07-09, bart <
bc@freeuk.com> wrote:
On 09/07/2024 16:58, Ben Bacarisse wrote:
bart <bc@freeuk.com> writes:
Arrays are passed by reference:
>
void F(int a[20]) {}
>
int main(void) {
int x[20];
F(x);
}
This is the sort of thing that bad tutors say to students so that they
never learn C properly. All parameter passing in C is by value. All of
it. You just have to know (a) what the syntax means and (b) what values
get passed.
>
The end result is that a parameter declared with value-array syntax is
passed using a reference rather than by value.
>
And it does so because the language says, not because the ABI requires
it. A 2-byte array is also passed by reference.
In C, arrays are not passed to functions, period.
Therefore ABIs do not say anything about array parameters,
(or if they do, it's not in relation to C).
An array can be wrapped in a struct: struct a { int a[42] }.
/That/ can be passed to a function, in the only way that C supports,
which is by value.
At the ABI level, passing of structs may be by address or copy,
and it may be dependent on the size. A small structure an be passed
as a parameter, or couple of parameters, which may be registers.
That's an implementation detail, all corresponding to "pass by value".
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca