Thread: Difference between passing *mycar and &mycar

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    102

    Difference between passing *mycar and &mycar

    I came across this in my book. Both of them look quite similar.

    void fixFlatTire(Car *myCar);
    void fixFlatTire(Car &myCar);

    The first one pass myCar to function fixFlatTire via a pointer.
    The second one pass myCar to function fixFlatTire via L-value of myCar. So, what's the different? Am I right, in explaining this way?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You would call the functions differently.
    Code:
    // in order of appearance:
    fixFlatTire(&myCar);
    fixFlatTire(myCar);
    What happens in these functions also looks different. The pointer version has this inside:
    Code:
    void fixFlatTire(Car *myCar)
    {
       if (myCar != NULL) {
          myCar->removeLugnuts();
          myCar->exchangeTire(new Tire);
          myCar->screwonLugnuts();
       }
       else {
          panic();
       }
    }
    The reference version...
    Code:
    void fixFlatTire(Car &myCar)
    {
       myCar.removeLugnuts();
       mycar.exchangeTire(new Tire);
       myCar.screwonLugnuts();
    }
    Maybe something like that? It's complicated...

    So that is the difference. Syntactically no big deal, but with the pointer version, you have to make sure that the pointer points to an object, and handle times when it doesn't. References are always an object with a different name, so there is nothing to really check, (unless you are too clever it won't break,) but references can't be reassigned a new object like pointers can. So pick whatever you need the variable to do.
    Last edited by whiteflags; 05-19-2012 at 11:35 PM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The difference within the function is syntax used to manipulate myCar. Within your first version, it is necessary to either dereference the pointer (to obtain a reference from the pointer) or use the -> operator to manipulate members or call member functions of myCar (it is also possible, as some amateurs do, to do (*myCar).operation in place of myCar->operation). Within your second version, it is necessary to use the . (dot) operator to manipulate members of myCar, or use the & operator to obtain a pointer from the reference.

    One important difference is that the second version (which accepts a reference) is allowed to assume myCar is a valid object (the caller has to avoid passing an invalid reference, or it is deemed to be invoking undefined behaviour). The pointer version can receive a NULL pointer, so that needs to be checked for.

    For the caller, the difference is in what can be passed. Given a reference to Car (or a variable of type Car) it is necessary to use the & operator to call the first version. Given a pointer to Car, it is necessary to dereference that pointer to call the second version.

    One other important difference is that the function can be called with an array of cars, and it is possible for the function to loop over those cars using array syntax. The second version cannot.
    Last edited by grumpy; 05-19-2012 at 11:33 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    thanks guys for the great explanation. Actually there's no big deal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-06-2011, 03:46 AM
  2. Replies: 6
    Last Post: 04-04-2010, 11:48 AM
  3. Struct question... difference between passing to function...
    By Sparrowhawk in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2009, 03:59 PM
  4. Replies: 16
    Last Post: 04-01-2008, 10:15 AM
  5. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 01:25 AM