:: [DNG] Reference in C -- was Re: Poi…
Αρχική Σελίδα
Delete this message
Reply to this message
Συντάκτης: Didier Kryn
Ημερομηνία:  
Προς: dng
Παλιά Θέματα: Re: [DNG] Pointer error in the backend of Netman
Αντικείμενο: [DNG] Reference in C -- was Re: Pointer error in the backend of Netman
Le 11/12/2015 09:01, aitor_czr a écrit :
> Hi Katolaz,
>
> Here you are an example of two arguments (x,y) passed by reference:
>
> # include <stdio.h>
>
> int main(void)
> {
>
> int i=1;
> int j=2;
>
> printf( "i = %d j = %d \n", i,j);
>
> func(&i, &j);
>
> printf( "i = %d j = %d \n", i,j);
>
> return 0;
>
> }
>
> void func(int *x, int *y)
> {
> int k = *x;
> *x = *y;
> *y = k;
> }
>
> This is the output:
>
> i = 1 j = 2
> i = 2 j = 1


     Aitor, you can tweak the things as you like but the fact is that 
the backbone of the design of the C language is that the API is as close 
as can be to the ABI. In terms of passing the argument, there is no 
other ABI than putting their values in registers and/or in the stack. 
Hence the arguments have the same status, for the called function, as 
automatic and/or register variables. The values they have when the 
function returns is lost.


     There is an exception for arrays. Arrays, in C, are not data types; 
the [] notation in a declaration merely allocates a range of memory; the 
same notation in instructions provides essentially another way to write 
pointer arithmetics and the array name itself is treated as a pointer.


     Reference is a concept of higher level languages; in addition to 
the address, it caries information about the data type and size. I must 
say the syntax is confusing, when using references, since it is often 
identical wether you use the variable or a refenrence to it. At least it 
is confusing to a C programmer who tends to associate reference and address.


     My one cent.


     Didier