Call by reference...

This is a discussion on Call by reference... within the C Programming forums, part of the General Programming Boards category; I have this program that asks you to input numbers, takes the average and prints it out.... its pretty easy ...

  1. #1
    Mak
    Mak is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    33

    Call by reference...

    I have this program that asks you to input numbers, takes the average and prints it out.... its pretty easy if you do by Value but i have to do it using Call by reference....so far i have :

    Code:
    #include <stdio.h>
    
    void INPUT(int *var1, int *var2, int *var3, int *var4);
    void AVERAGE(int var1, int var2, int var3, int var4,float *var5);
    void OUTPUT(float *var); 
    
    main(){
    
    int x,y,z,v;
    float sum, output;
    
    float *psum, *poutput;
    
    psum=&sum;
    poutput=&output;
    
    
    INPUT(x, y,z,v);
    AVERAGE(x, y,z,v,psum);
    OUTPUT(poutput);
    
    }
    
    void INPUT(int *var1, int *var2, int *var3, int *var4){
    
    printf("Please enter four integers : ");
    scanf("%d%d%d%d", *var1, *var2, *var3, *var4 );
    
    
    }
    
    void AVERAGE(int var1, int var2, int var3, int var4,float *var5){
    
    *var5=(var1+ var2 + var3 + var4)/4;
    }
    
    
    void OUTPUT(float *var){
    printf("Average of the 4 integers is %5.2f \n", *var);
    }

    i'm not sure what this error is about....




    programs\lab5.c:21: warning: passing arg 1 of `INPUT' makes pointer from integer without a cast
    programs\lab5.c:21: warning: passing arg 2 of `INPUT' makes pointer from integer without a cast
    programs\lab5.c:21: warning: passing arg 3 of `INPUT' makes pointer from integer without a cast
    programs\lab5.c:21: warning: passing arg 4 of `INPUT' makes pointer from integer without a cast

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    7
    try this


    INPUT (&x, &y, &z, &v);

  3. #3
    Mak
    Mak is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    33
    Code:
    #include <stdio.h>
    
    void INPUT(int *var1, int *var2, int *var3, int *var4);
    void AVERAGE(int var1, int var2, int var3, int var4,float *var5);
    void OUTPUT(float *var); 
    
    main(){
    
    int x,y,z,v;
    float sum, output;
    
    float *psum, *poutput;
    
    psum=&sum;
    poutput=&output;
    
    
    INPUT(&x, &y,&z,&v);
    AVERAGE(x, y,z,v,psum);
    OUTPUT(poutput);
    
    }
    
    void INPUT(int *var1, int *var2, int *var3, int *var4){
    
    printf("Please enter four integers : ");
    scanf("%d%d%d%d", &var1, &var2, &var3, &var4 );
    
    
    }
    
    void AVERAGE(int var1, int var2, int var3, int var4,float *var5){
    
    *var5=(var1+ var2 + var3 + var4)/4;
    }
    
    
    void OUTPUT(float *var){
    printf("Average of the 4 integers is %5.2f \n", *var);
    }

    it compiles now but it gives me 0 as an output ....what am i missing?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > scanf("%d%d%d%d", &var1, &var2, &var3, &var4 );
    They're already pointers (within this function), so
    scanf("%d%d%d%d", var1, var2, var3, var4 );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Mak
    Mak is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    33
    yeah baby i fixed it


    changed OUTPUT(poutput); to

    OUTPUT(psum);

    i guess that make sense.....although now i dont get float values


    i'll figure that out...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 03:48 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  4. call by reference
    By Jackie in forum C Programming
    Replies: 3
    Last Post: 10-29-2001, 05:46 AM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21