Passing by reference/using pointers

This is a discussion on Passing by reference/using pointers within the C++ Programming forums, part of the General Programming Boards category; I've just started C++ and am trying to get to grips with pointers, consider the following 2 programs: Code: void ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    145

    Passing by reference/using pointers

    I've just started C++ and am trying to get to grips with pointers, consider the following 2 programs:

    Code:
    void change(int &i)
    {
         i = 5;
    }
         
    int main()
    {
        int i = 0;
        change(i);
        printf("%d\n",i);
    }
    Code:
    void change(int *i)
    {
         *i = 5;
    }
         
    int main()
    {
        int i = 0;
        change(&i);
        printf("%d\n",i);
    }
    The first one passes the reference of i, and the second passes up a pointer to i(correct me if I'm wrong)

    What is the difference between the two methods of passing variables? As far as I can see, both edit the value of i directly. Why are pointers more useful?

    Thanks

  2. #2
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    There is no difference really, you're just writing the address/pointer conversions in different places.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > Why are pointers more useful?
    Shouldn't that be why are references more useful?

    A reference is always valid, whereas a pointer may be NULL, or garbage.

    A reference is type-safe, whereas a pointer can be coerced into only appearing to be the right type.
    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.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Hmm ok.

    In what situations are pointers more useful than references then?

  5. #5
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by Wiretron View Post
    Hmm ok.

    In what situations are pointers more useful than references then?
    One occasion is when you need an "empty" value. In that occasion you use a pointer set to NULL.

  6. #6
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    the difference a pointer and a reference is that references have cleaner syntax plus less memory taken compared to pointer
    but i use pointers anyway to have the complete functionality of the memory. And when using pointers, be careful not to dereference a pointer that doesn't point to anything.
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    Quote Originally Posted by Hussain Hani View Post
    the difference a pointer and a reference is that references have cleaner syntax plus less memory taken compared to pointer
    "Cleaner syntax" is subjective: not everyone's interpretation of "clean" is the same. There is no particular reason that use of references would consume either more or less memory than use of pointers.

    A reference, once created, cannot be reseated (i.e. made to refer to something else). A pointer can. Changing the value of a reference (eg assignment, iterating it) always affects the object referred to, whereas changing the value of a pointer reseats the pointer. For example;
    Code:
    int main()
    {
         int x[2];
         int &refx(x[0]);
         int *px(&x[0]);
    
         x[0] = 5;   x[1] = 10;   // some arbitrary values in x
    
         ++refx;    // sets x[0] to be 6
         ++px       //   changes px so it points at x[1]  (reseating the pointer)
    
         refx = x[2];   // assigns x[0] so it has the value in x[1]   (10)     
         *px = 5;        //   sets x[1] to be 5, but x[0] is still 10
         
    }
    One effect of this is that pointers can be used to work on arrays (eg incrementing a pointer makes it point to the next element in an array, therefore the joys of pointer arithmetic. References cannot be used in a similar manner.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    Quote Originally Posted by grumpy View Post
    "Cleaner syntax" is subjective: not everyone's interpretation of "clean" is the same. There is no particular reason that use of references would consume either more or less memory than use of pointers.

    A reference, once created, cannot be reseated (i.e. made to refer to something else). A pointer can. Changing the value of a reference (eg assignment, iterating it) always affects the object referred to, whereas changing the value of a pointer reseats the pointer. For example;
    Code:
    int main()
    {
         int x[2];
         int &refx(x[0]);
         int *px(&x[0]);
    
         x[0] = 5;   x[1] = 10;   // some arbitrary values in x
    
         ++refx;    // sets x[0] to be 6
         ++px       //   changes px so it points at x[1]  (reseating the pointer)
    
         refx = x[2];   // assigns x[0] so it has the value in x[1]   (10)     
         *px = 5;        //   sets x[1] to be 5, but x[0] is still 10
         
    }
    One effect of this is that pointers can be used to work on arrays (eg incrementing a pointer makes it point to the next element in an array, therefore the joys of pointer arithmetic. References cannot be used in a similar manner.
    Well Put..

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by Wiretron View Post
    The first one passes the reference of i, and the second passes up a pointer to i(correct me if I'm wrong)

    What is the difference between the two methods of passing variables? As far as I can see, both edit the value of i directly. Why are pointers more useful?
    This might trigger a huge debate. I try to advocate completely pointer-less programming in C++, although there are times when there's simply no other option but to use a pointer. In most cases it is unnecessary, especially if you take full advantage of the containers provided by STL.

    At the level of the underlying functionality of the language, a pointer and a reference are pretty much the same thing. At the syntactic level, they work differently. A pointer is a variable that can be assigned and reassigned at will. You can use the same pointer variable to point at one object, then change it to point at another. A reference can't do this. All references are "bound" to an object the moment they come into existence. This eliminates a host of problems centering around uninitialized pointers.

    I'd suggest that if you don't see why you need pointers, then you don't need pointers. Stick with references until you bump into one of the cases where they simply aren't enough.

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by Salem View Post
    > Why are pointers more useful?
    Shouldn't that be why are references more useful?
    Pointers are strictly more powerful. I don't know about "more useful." The most common case I can think of where pointers are required would be a container of polymorphic objects. Most STL containers require that their contents be default-constructible. A reference is not default-constructible so it is impossible to use references to implement containers of polymorphic types. You simply have to use pointers in that case.

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Deep down, for function calls, pointers and references are more or less the same, as the function does need the address of the variable to work on it. I hope no one mistakes what I'm saying here. I don't think they should be interchanged whenever a programmer might feel like it.

    The importance of references is a higher-level concept for the sake of the programmer and users.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a string array to a function using pointers
    By asofaihp in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2009, 12:31 PM
  2. Passing Pointers by reference
    By Bladactania in forum C Programming
    Replies: 10
    Last Post: 02-13-2009, 10:14 AM
  3. passing pointers outside of functions
    By thomas41546 in forum C Programming
    Replies: 6
    Last Post: 01-26-2008, 06:00 PM
  4. Passing pointers
    By cyberCLoWn in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2004, 01:17 PM
  5. Pointers and reference passing
    By Denis Itchy in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2002, 01:36 AM

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