:: Re: [DNG] Studying C as told. (For …
Inizio della pagina
Delete this message
Reply to this message
Autore: KatolaZ
Data:  
To: dng
Oggetto: Re: [DNG] Studying C as told. (For help)
On Wed, Jun 22, 2016 at 05:04:38PM +0200, Edward Bartolo wrote:

[cut]

>
> This is why I am finding it hard to logically accept that "passing by
> reference" is NOT equivalent to passing a pointer for the machine. For
> the programmer, it is using a different syntax, but the machine sees
> no difference. And since, in the end what programmers do with their
> code is running it on a machine, what a machine sees is what counts,
> at least for me.
>


There is a conceptual difference though, and a quite important one. It
is perfectly legal in C to do something like:

=====

int fun (int *v){

    v[4] = 17;
}


int fun2(char *p){

    p[32] = 0;


}

int fun3(float *f){

    f[7] = 3.1415;
}



int main(){

    int *a;


    a = malloc(47 * sizeof(double)); // Yes, I know I wrote "sizeof(double)" here, so what?


    fun(a);
    fun(a+17);
    fun(a+37);


    fun2((char*)a);
    fun2((char*)(a + 21));


    fun3((float*)a);


}

====

and the reason is that arrays in C are just syntactic sugar around
memory addresses. This has nothing to do with polymorphism or generic
programming, and is indeed much lower-level and more basic than
that. Simply put, arrays do not effectively *exist* in C.

We are getting more into phylosophy here, but if you keep the
misconception that "arrays are passed by reference" you are making two
logical mistakes. The first one is that there is no thing like
"passing-by-reference" in C, and the second one is that no array can
be actually "passed" to function, since an "array" is just a bunch of
contiguous memory locations that you have allocated, and to which you
have attached some particular "meaning".

The only things that can be "passed" to C functions are primitive type
variables, structures, and pointers. That's it.

>From C's perspective, the content of an "array" has no particular

meaning at all. You can address each single byte of it, or copy entire
unaligned chunks to another memory area, and the language will not
stop you from doing that, because there is no such thing like "the
size of each of the elements of an array". That's just in your
mind. You can also go beyond the "boundaries" of those "arrays", and
the language will not interfere, because there is no such thing like
"the boundaries of an array". The boundaries exist only in your
mind. Arrays exist only in the programmer's mind.

This is a stark difference between C and other languages, e.g. Pascal,
and is a fundamentally important one to understand, IMHO.

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  ]