Thread: problem with variable passed as reference

  1. #1
    Registered User
    Join Date
    Apr 2006

    problem with variable passed as reference

    This is completely confusing me. I have a call to a function:
     case '5': if ((findfree((int)&roomnum)) < 0)
                                                      printf("No available rooms\n");
                                                      printf("%d is available\n", roomnum);
    and it leaves roomnum with a 10 digit number (when to me) the definition of the function looks like it should give the varaiable roomnum either 1-10 or return -1..

    #include "hotel.h"
    #include <unistd.h>
    #include <sys/types.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include <stdbool.h>
    int fd;
    char buf[NLINES];
    findfree(int room)
        ssize_t nread;
        int i, j;
        _Bool vacant;
        if (lseek(fd, (off_t)0, SEEK_SET) < 0)
            perror("problem with lseek in findfree: line 23");
        for (i = 1; i <= MAXROOMS; ++i) {
            if ( (nread = read(fd, buf, sizeof(buf))) < 0)
                perror("problem with read in findfree function: line 21"), exit(-1);
            vacant = true;
            for (j = 0; j < nread; ++j) {
                if (isalpha(buf[j])) {
                    vacant = false;
            if (vacant == true) {
                room = i;
                printf("***%d\n\n", room);
                return 1;
        return -1;
    Anyone see what's wrong with this?

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Banks of the River Styx
    Your function seems to return 1 on success, -1 on failure. That's it: no room number. It does store a room number to it's local copy (room = i), but this is lost when the function returns. C is a pass by value lanugage... when you say, function(myvar), the function receives the value of myvar in a new variable, not the variable itself.

    What your code does is pass not the variable roomnum or its value to the function, but rather the variable's address in memory (type int *, which is incorrectly cast to an int)...

    C++ has pass by reference, it looks like this:
    int myfunc(int &var)
       var = 2; // Changes to var will appear outside this function.
    As opposed to:
    int myfunc(int var)
      var = 2; // The assignment is only visible to this function.
    In C, you can use pointers to accomplish the same idea:
    int myfunc(int *var)
      *var = 2; // var will be 2 outside the function.
    Read the FAQ on pointers.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

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. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. Problem with Makefile
    By pinkprincess in forum C Programming
    Replies: 3
    Last Post: 06-24-2007, 09:02 AM
  4. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM