Path: ...!news.nobody.at!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: bart Newsgroups: comp.lang.c Subject: =?UTF-8?Q?Re=3A_technology_discussion_=E2=86=92_does_the_world_need?= =?UTF-8?B?IGEgIm5ldyIgQyA/?= Date: Wed, 10 Jul 2024 16:40:29 +0100 Organization: A noiseless patient Spider Lines: 50 Message-ID: References: <87wmlzvfqp.fsf@nosuchdomain.example.com> <87h6d2uox5.fsf@nosuchdomain.example.com> <20240707164747.258@kylheku.com> <877cdur1z9.fsf@bsb.me.uk> <871q42qy33.fsf@bsb.me.uk> <87ed82p28y.fsf@bsb.me.uk> <87r0c1nzjj.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 10 Jul 2024 17:40:30 +0200 (CEST) Injection-Info: dont-email.me; posting-host="ded9e1848a26cfa2c70264cde0490f0f"; logging-data="2080738"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18h8O0A+buBZ60hxBYf7vob" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:d+tZCJdWKhgg9G9rnZbyA2JgilE= Content-Language: en-GB In-Reply-To: Bytes: 3382 On 10/07/2024 15:54, Janis Papanagnou wrote: > Values passed (including values of pointers [used for arrays]) are > handled (in the functions) as copies and cannot change the original > entities (values or dereferenced objects) in the calling environment. > > To make it possible to change entities in the calling environment > in "C" you have to implement the necessary indirection by pointers. You don't have to do anything at all: #include typedef unsigned char byte; typedef byte vector[4]; void F(vector a) { a[0]+=3; a[1]+=3; } int main(void) { vector v = {10,20,30,40}; printf("%d %d %d %d\n", v[0], v[1], v[2], v[3]); // 10 20 30 40 F(v); printf("%d %d %d %d\n", v[0], v[1], v[2], v[3]); // 13 23 30 49 } Here it looks superficially as though 'v' is passed by value (and it is of a size that the ABI /would/ pass by value), yet F changes its caller's data, perhaps unintentionally. > Your insistence is amazing. /I/ am amazed at everyone's insistence that there is nothing remarkable about this, and that it is nothing at all like pass-by-reference. So, how do I write F in C so that the caller's data is unchanged? Sure, true pass-by-reference has some extra properties, but if I wanted to duplicate the behaviour of the above in my language, I have to use pass-by-reference. In C you get that behaviour anyway (possibly to the surprise of many), in a language which only has pass-by-value, and without needing explicit pointers. That really is remarkable. And not unsafe at all!