Liste des Groupes | Revenir à cl c |
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.
Nobody has said anything close to "it is nothing at all like pass-by-reference". Many people have made it clear that using pointers - explicitly, or implicitly via the language's automatic decay of array and function expressions to pointers in certain contexts, can be used to give a similar effect to passing by reference. Indeed, it is precisely the reason C does not have "pass by reference" - it is not needed, as you can conveniently use pointers and "pass by value".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?You use a type that you /can/ pass as an argument in C. You can't pass arrays to functions, or return them. A convenient method is to wrap the array in a struct :
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.I really don't think any of this is a surprise to many people who make more than a basic effort to learn the language. It is not hard to understand, as long as you don't fixate on wrong interpretations of how it all works in C.
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!C is a language that requires more responsibility by the programmer for things that higher level languages handle automatically. That's the way the language is. If you don't understand it, you'll make mistakes.
Les messages affichés proviennent d'usenet.