Thread: Trouble with passing by reference

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    1

    Trouble with passing by reference

    Hello everyone, I'm very new to C and programming in general. Apologies if this is a very obvious mistake but I'm stuck and not sure what to do. I'm trying to do a set function on one of my variables and when I pass my int pointer and change the value in the function. The int in my main program always turns out to be 1 for whatever reason.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include"employee.h"
    
    
    void setNumberOfEmployees(int* numEmployees);
    
    
    int main()
    {
        int employees; // amount of employees that signed up for LWOP
        int* pEmployees = &employees;
        setNumberOfEmployees(pEmployees);
    
    
        printf("%d", employees);
    
    
        struct employee lwopListed[employees];
    
    
        return 0;
    }
    
    
    void setNumberOfEmployees(int* numEmployees) {
        int employees = scanf("%d", &employees);
        if (employees > 0)
            *numEmployees = employees;
        return;
    }
    /* output
    23 // this is the number I entered in the setEmployeeNumber function
    1 // and this is the value of employees after the function
    Process returned 0 (0x0)   execution time : 3.260 s
    Press any key to continue.
    */

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Code:
    #include <stdio.h>
    
    void setNumberOfEmployees(int *numEmployees);
    
    int main() {
        int employees;
        setNumberOfEmployees(&employees);
        printf("%d\n", employees);
        return 0;
    }
    
    void setNumberOfEmployees(int *numEmployees) {
        *numEmployees = 0;
        scanf("%d", numEmployees)
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > int employees = scanf("%d", &employees);
    The last value assigned to employees is the return result of scanf.

    This is what scanf returns.
    RETURN VALUE
    On success, these functions return the number of input items successfully matched and assigned; this can
    be fewer than provided for, or even zero, in the event of an early matching failure.

    The value EOF is returned if the end of input is reached before either the first successful conversion or
    a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator
    for the stream (see ferror(3)) is set, and errno is set to indicate the error.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing by reference of by value?
    By abd in forum C Programming
    Replies: 32
    Last Post: 01-28-2010, 04:49 PM
  2. passing by reference
    By goran00 in forum C Programming
    Replies: 6
    Last Post: 03-21-2008, 01:03 PM
  3. passing reference more than once
    By l2u in forum C++ Programming
    Replies: 1
    Last Post: 12-17-2006, 10:03 AM
  4. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  5. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 01:25 AM

Tags for this Thread