pass by value / passy by reference

This is a discussion on pass by value / passy by reference within the C Programming forums, part of the General Programming Boards category; I'm experimenting with pass by value/reference and im having trouble maybe someone can help. ill just post a simplified example ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    66

    pass by value / passy by reference

    I'm experimenting with pass by value/reference and im having trouble maybe someone can help.
    ill just post a simplified example of the problem i am having. i know it has something to do with pointers but everything i try doesent work.

    Function call
    Code:
           readname (fin, a, emp);
    emp has no value when brought into function

    function
    Code:
    void readname(FILE* fin, struct emp a[1024], int num_emp) {        
            
            fscanf(fin, "%d", &num_emp);        
           
            printf("%d", num_emp);
    }
    num_emp = 2 when it prints in the function

    then in main i print emp right after this function runs to check its value... ant it's 0 again.

    do i need an ampersand somewhere?

    thanks for the help.
    Last edited by ominub; 04-17-2009 at 11:33 PM.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    You need to pass a pointer to emp if you want the called function to change its value in the calling function.

    Code:
    readname (fin, a, &emp);
    Code:
    void readname(FILE* fin, struct emp a[], int *num_emp) {        
            
            fscanf(fin, "%d", num_emp);        
           
            printf("%d", *num_emp);
    }
    By the way, the size of the array is unnecessary; the compiler will treat struct emp a[1024], struct emp a[], or struct emp *a as just a pointer to a struct emp. Also, although I guess it's legal to have both a struct emp and an int emp in your program, it seems unnecessarily confusing. Why not use a different (and more descriptive) name for the int, such as int num_emp as you did in readname()?

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    Quote Originally Posted by R.Stiltskin View Post
    Also, although I guess it's legal to have both a struct emp and an int emp in your program, it seems unnecessarily confusing. Why not use a different (and more descriptive) name for the int, such as int num_emp as you did in readname()?

    i have it named that in the actual code i just changed it to set the function call apart from the function more clearly. i didnt realize i made the struct and the int both the same name.

    i tried exactly what you posted a few times and i get the same output plus a [warning] "comparison between pointer and integer"

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by ominub View Post
    i tried exactly what you posted a few times and i get the same output plus a [warning] "comparison between pointer and integer"
    That does not relate to the code we have posted here. There must be something wrong elsewhere in the program (the warning should tell you which line). In the code I posted, I assumed that in the calling function, num_emp is defined as
    int num_emp, NOT int *num_emp. Which is it?

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    i wont change anything this time other than shorten it. i thought changing it would help but it only made it worse i guess. here...


    Code:
    void readempname(FILE* fin, struct employee a[1024], int num_emp) ;
    
    int main() {
    
        int emp;
    
            readempname(fin, a, emp);
    
            printf("%d\n", emp);
    
    }
    
    
    void readempname(FILE* fin, struct employee a[1024], int num_emp)  {
    
    int     i=0;
        
            // read in  # of employees
            fscanf(fin, "%d", &num_emp);
            
            //read in first name / last name / pph
            for (i = 0; i < num_emp; i++) {         <--------- Getting warning on this line after i apply your suggested changes.
                fscanf(fin, "%s", a[i].first);
                fscanf(fin, "%s", a[i].last);
                fscanf(fin, "%lf", &a[i].payperhr);
            }
    
            printf("%d\n", num_emp);
    }

    output is:

    2
    0




    did that make more sense? that is what i have now.
    Last edited by ominub; 04-17-2009 at 10:29 PM.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Warning is because when you pass &emp to the function, you have to use *num_emp for the comparison.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    then it just crashes and doesn't print anything. no errors or warnings just a pop up window that says the program has stopped working.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Post the code that crashed.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    PM okay? its an open assignment.

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Just post the same section as before, after the changes you made.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    Code:
    void readempname(FILE* fin, struct employee a[1024], int *num_emp) ;
    
    int main() {
    
        int emp;
    
            readempname(fin, a, &emp);
    
            printf("%d\n", emp);
    
    }
    
    
    void readempname(FILE* fin, struct employee a[1024], int *num_emp)  {
    
    int     i=0;
        
            // read in  # of employees
            fscanf(fin, "%d", &num_emp);
            
            //read in first name / last name / pph
            for (i = 0; i < *num_emp; i++) {         
                fscanf(fin, "%s", a[i].first);
                fscanf(fin, "%s", a[i].last);
                fscanf(fin, "%lf", &a[i].payperhr);
            }
    
            printf("%d\n", num_emp);
    }

  12. #12
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    The only error I see there is that you forgot to change num_emp to *num_emp in the last printf statement, but that shouldn't cause it to crash. I suggest you move that printf statement to immediately after the fscanf that reads in the value for num_emp, just to be sure that the input is OK (it should print before it crashes). Then you can start looking for the next error someplace else.

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    i put the printf right after the fscanf and it still crashed without printing num_emp

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    i tried something a little different and it works but its a little sketchy... what do you think?

    Code:
    void readempname(FILE* fin, struct employee a[1024], int *num_emp) ;
    
    int main() {
    
        int emp;
    
    
            // read in  # of employees
            fscanf(fin, "%d", &emp);
    
            readempname(fin, a, &emp);
    
            printf("%d\n", emp);
    
    }
    
    
    void readempname(FILE* fin, struct employee a[1024], int *num_emp)  {
    
    int     i=0;
        
            // i took the fscanf out of the function
            
            //read in first name / last name / pph
            for (i = 0; i < *num_emp; i++) {         
                fscanf(fin, "%s", a[i].first);
                fscanf(fin, "%s", a[i].last);
                fscanf(fin, "%lf", &a[i].payperhr);
            }
    
            printf("%d\n", *num_emp);
    }

    aparently, you arent supposed to have scanf s to replace the variable passed in when you are using pass by reference. but isnt that the point? pass by ref is confusing.
    Last edited by ominub; 04-17-2009 at 11:17 PM.

  15. #15
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Sorry, I just looked again at the code you posted in #11. You also forgot to change the fscanf statement in readempname(). Look at my code in post #2 more carefully. num_emp is already a pointer, so you shouldn't be using the & operator there.

Page 1 of 2 12 LastLast
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, 09:18 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 05:42 PM
  5. how can i pass by reference by "malloc 2d array"?
    By Mathsniper in forum C Programming
    Replies: 10
    Last Post: 05-22-2005, 03:23 PM

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