C - Pass by reference to pointers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

 The following C code snippet uses some sort of reference type
 that I am unaware of.I recently came across this during multithreaded 
 programming.

 void f(int *t) {
        printf("%u\n", t);
 }
                                                                                
 void main() {
        int i=2;
                                                                                
        printf("%u\n", &i);
                                                                                
        f((int*)i);     ---- > case 1 : gives output 2
        f(&i);	        ---- > case 2 : normal
 }


 Usually, (well I thought so) the argument expected in f() is a pointer 
 variable. The above gives the result as 2 for the first call to f()!
 Why is t giving out 2 without being dereferenced in case 1 ?

 How come case 1 and case 2 are both accepted for a calling same 
 function ? I mean same prototype appears to have different meaning
 based on the callee. Any advice ?

 -----------------------------------------------------------------
 gcc pointer_ref.c -o pointer_ref
 ./pointer_ref

 3221202660
 2
 3221202660

 As usual my gcc version is 3.2.3.
 

 

-- 
Regards,
Anitha B
@S A N K H Y A


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux