This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

C - Pass by reference to pointers


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 Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]