:: Re: [DNG] Studying C as told. (For …
Góra strony
Delete this message
Reply to this message
Autor: KatolaZ
Data:  
Dla: dng
Temat: Re: [DNG] Studying C as told. (For help)
On Wed, Jun 22, 2016 at 11:15:44AM +0200, Edward Bartolo wrote:
> Hi,
>
> Now I have several exercises from "The C programming language"
> (Kernighan & Ritchie). Some question look rather challenging...
>
> I learnt arrays are always passed by reference to function calls. So,
> I wrote the following program to make use of this so that a variable
> in main() is modified in a function call.


We have already debated this point, but gain, this is a misconcenption
that you have to abandon, because it's *wrong*.

There is no "passing by reference" in C. Forget it. No
references. Only *values*. Everything is passed *by* *value* in C. You
will never pass "an array" to a function call in C. You will instead
pass the *value* of a *pointer* to a contiguous region of RAM which
contains the elements of your array. You pass *an adress*, namely the
address of the first position in RAM where your array stays, not "the
array", or a reference to the array. And this is implemented by
copying on the stack the *value* of the pointer (i.e., the address),
so that your function can access the array that start at that address.

You *never* pass anything by reference in C. Never ever. C has only
pass-by-value.

HND

KatolaZ

-- 
[ ~.,_  Enzo Nicosia aka KatolaZ - GLUGCT -- Freaknet Medialab  ]  
[     "+.  katolaz [at] freaknet.org --- katolaz [at] yahoo.it  ]
[       @)   http://kalos.mine.nu ---  Devuan GNU + Linux User  ]
[     @@)  http://maths.qmul.ac.uk/~vnicosia --  GPG: 0B5F062F  ] 
[ (@@@)  Twitter: @KatolaZ - skype: katolaz -- github: KatolaZ  ]