Deutsch English Français Italiano |
<v6k6i0$1h4d3$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!feeds.phibee-telecom.net!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: bart <bc@freeuk.com> Newsgroups: comp.lang.c Subject: =?UTF-8?Q?Re=3A_technology_discussion_=E2=86=92_does_the_world_need?= =?UTF-8?B?IGEgIm5ldyIgQyA/?= Date: Tue, 9 Jul 2024 21:28:49 +0100 Organization: A noiseless patient Spider Lines: 80 Message-ID: <v6k6i0$1h4d3$1@dont-email.me> References: <v66eci$2qeee$1@dont-email.me> <v67gt1$2vq6a$2@dont-email.me> <v687h2$36i6p$1@dont-email.me> <871q48w98e.fsf@nosuchdomain.example.com> <v68dsm$37sg2$1@dont-email.me> <87wmlzvfqp.fsf@nosuchdomain.example.com> <v6ard1$3ngh6$4@dont-email.me> <v6b0jv$3nnt6$1@dont-email.me> <87h6d2uox5.fsf@nosuchdomain.example.com> <v6d779$6rk5$2@dont-email.me> <v6e76u$c0i9$1@dont-email.me> <v6esqm$fian$2@dont-email.me> <v6f7vg$hgam$1@dont-email.me> <20240707164747.258@kylheku.com> <v6gl83$s72a$1@dont-email.me> <v6h8ao$ur1v$1@dont-email.me> <v6jhk3$1drd6$1@dont-email.me> <v6jiud$1dsjb$1@dont-email.me> <877cdur1z9.fsf@bsb.me.uk> <v6joi4$1epoj$1@dont-email.me> <871q42qy33.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 09 Jul 2024 22:28:49 +0200 (CEST) Injection-Info: dont-email.me; posting-host="7c22da865deaeb7152beebd5e4e580c3"; logging-data="1610147"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ON5olEa69xNq3KArRFm66" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:Pa++iOoI3MW6Yvtb+9ifQri1ZBA= In-Reply-To: <871q42qy33.fsf@bsb.me.uk> Content-Language: en-GB Bytes: 4220 On 09/07/2024 18:22, Ben Bacarisse wrote: > bart <bc@freeuk.com> writes: > >> 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. > > An address value is passed by value. C has only one parameter passing > mechanism. You can spin it as much as you like, but C's parameter > passing is simple to understand, provided learner tune out voices like > yours. Little about C's type system is simple. You're doing your students a disservice if you try and hide all the quirks. There's a discontinuity in the type system when it comes to parameter types (T is is non-array typedef; U is an array typedef): Non-Param Same type as Param T a T a T a[10] T *a T a[10][20] T (*a)[20] U a U *a U a[10] U (*a)[?] ? is the length of U's array U A[10][20] U (*a)[20][?] Any top-level array type in a parameter (that is, its description in English starts with 'array of'), is converted to a pointer type (now starts with 'pointer to'). But the lower levels are not affected. If you were to try the same exercise in one of my languages, if would be a smaller table: Param/Non-Param, T is array/non-array T a T a[10] T a[10][20] > Sorry, I missed what you wrote. I don't know why even brought up int > (*)[20] but I thought you were saying that was the type of a. int(*)[20] would be the type of 'a' inside my function, IF it was properly passed by value instead of some weird combination. If I define this function in one of my languages, where '&' means pass-by-reference: proc f([20]int32 &A)= end and tell it to transpile to C, it produces (minus the name decoration): static void f(i32 (*a)[20]) { }