Sujet : Re: technology discussion → does the world need a "new" C ?
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.cDate : 16. Aug 2024, 09:04:31
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v9n16f$1c6af$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:102.0) Gecko/20100101 Thunderbird/102.11.0
On 15/08/2024 18:08, Bart wrote:
These were my original comments on the subject made to DB:
DB:
>. In C, there is no "pass by reference" or "return by reference". It is all done by value.
BC:
>Arrays are passed by reference:
> void F(int a[20]) {}
> int main(void) {
> int x[20];
> F(x);
> }
> Although the type of 'a' inside 'F' will be int* rather than int(*)[20].
It was in reply to DB which appear to imply that arrays were passed by value. Obviously they're not passed by value, so what then? (Please, don't rerun the thread! This is where everyone jumped in.)
I am not sure if you want an answer here or not - you asked "so what then", but also asked to avoid a re-run of the thread.
I can give a summary - and I also hope this doesn't lead to a re-run of the discussion. However, since you are asking the same question as you did at the start, and the language C has not changed in the meantime, the factual and correct answers will inevitably be the same:
1. C has no "pass by reference" - it is all "pass by value".
2. In C, you cannot pass an array as a function parameter.
3. The automatic conversion of many array expressions to pointer expressions, along with the similar conversions of function parameter types, gives C users a syntax that is similar - but not identical to - what you would have if the language supported passing arrays by reference.
4. Adding "pass by reference" and "arrays as first class objects" would both be very significant changes to C - the language, the tools, and the way C code is written. Doing so in a way that significantly improves on the current situation (such as including element count information when passing arrays) would require even more changes. Either array objects would need to hold run-time count information (changing the data held, massively complicating "slices", and introducing run-time efficiency overheads), or the information would need to be part of the array's type, requiring templates, overloaded functions, and other mechanisms. Both are viable techniques for other languages, but not appropriate for C.
The C solution to handling arrays has its limitations, and has potential to cause some people significant confusion. It is important to be careful in your coding, and to make use of tools that help spot errors, but that applies to all programming in all languages. The C solution works well in practice for many situations, with minimal complications and maximal run-time efficiency.