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 : 10. Jul 2024, 04:19:10
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240709201324.49@kylheku.com>
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 : slrn/pre1.0.4-9 (Linux)
On 2024-07-09, bart <
bc@freeuk.com> wrote:
On 09/07/2024 23:32, Kaz Kylheku wrote:
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.
>
Arrays can be passed by explicit reference:
The C term for "explicit reference" is "pointer".
C has neither "pass by pointer" nor "pass by explicit reference"; it has
pointers passed by value.
Pass by reference/pointer looks like this:
void f(var int x)
{
x++:
}
int main(void)
{
int x = 42;
f(x);
// x is now 43
}
That's like a var parameter in Pascal or C++ references (where our
fantasy syntax looks like void f(int &x).
Pass by reference means the callee can assign to the parameter
and the caller's object changes. (Unless it's some kind of constant
reference or something.)
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca