CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2006
    Location
    United States
    Posts
    112

    Pointers, passing by reference, and good 'style'

    I was reading on pointers in a PDF I found on the tubes, and I noticed that a code example uses pointers to do a pass by reference.

    In C++ I last spring, when we wanted to pass a value by reference, we used int& someVar; for the variable in the parameter. The PDF uses int* someVar; instead. As far as I know, they do the same thing with no extra abilities given / taken from each one. Which is the more common, or preferred method?

    Also, are pointers just like reference variables but involving two named variables (pointer, pointee), and not constricted to a parameter, or is there more?


    M.
    Programmer in the making...

  2. #2
    Join Date
    Aug 2005
    Posts
    132

    Re: Pointers, passing by reference, and good 'style'

    Pointers and references are two different things. Conceptually a pointer is a memory location holding a pointer to the area of memory that holds the data you require. A reference is the memory location of the data you require.

    There are instances where both are valid, however int *someVar is passing a pointer to a valuea nd int &someVar is passing a value by reference. I suppose the difference would be that when passing a pointer you can change the address to which that pointer points where as when passing a reference you can only change the values associated with that memory address.

    Hope that helps.

    Cheers
    Odd

  3. #3
    Join Date
    Oct 2007
    Posts
    178

    Re: Pointers, passing by reference, and good 'style'

    the other difference is how you change the value. e.g. in a very basic function that swaps two numbers:
    Code:
    void swapnum_bypointer(int *i, int *j) {
    	int temp = *i;
    	*i=*j;
    	*j=temp;
    }
    Code:
    void swapnum_byreference(int &i, int &j) {
    	int temp = i;
    	i=j;
    	j=temp;
    }
    if you use i instead if *i in the first function you will alter the address, not the value as stated above.

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Pointers, passing by reference, and good 'style'

    Plus, a pointer can legally have the value 0 (pointing to nothing). A reference must refer to some valid entity.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Pointers, passing by reference, and good 'style'

    The main benefit of passing pointers rather than references is that it makes it much clearer which parameters are inputs and which are outputs.

    In fact, the only time I used pass-by-reference I also use const (if at all possible given controllable code), just to make it plain that the thing isn't an output. I only do it to avoid unnecessary copies on potentially bulky objects.

  6. #6
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: Pointers, passing by reference, and good 'style'

    Quote Originally Posted by GuOddian
    I suppose the difference would be that when passing a pointer you can change the address to which that pointer points where as when passing a reference you can only change the values associated with that memory address.
    Yes, but note that changing the value of the pointer (he address to which that pointer points) will not reflect in the calling function because the pointer itself is passed by value.
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  7. #7
    Join Date
    Jun 2006
    Location
    United States
    Posts
    112

    Re: Pointers, passing by reference, and good 'style'

    I see. So I mixed that up? The way it sounded was that it essentialy achieves the same goal of being able to change data without a return _____;.

    M.
    Programmer in the making...

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Pointers, passing by reference, and good 'style'

    No, you're right about that. You can change the data the pointer points at. You merely can't change the pointer itself.

    Which is why God invented pointers to pointers.

  9. #9
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Pointers, passing by reference, and good 'style'

    In short, you can say that reference to equivalent to a constant pointer other than the syntax used is much neater.

    BTW, people with background in C and haven't fully understand references, tend to use pointer. Without declaring the pointer as constant, the compiler will not be able to flag out error when the pointer is unintentionally modified.
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  10. #10
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Pointers, passing by reference, and good 'style'

    Just to point this out: References are a C++ concept, so if the pdf you've been reading is about C and not C++, it would have to use pointers to pass a variable by reference.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  11. #11
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    Re: Pointers, passing by reference, and good 'style'

    IMO, pass-by-reference and pass-by-pointer accomplish the same thing. Mainly, to be able to modify/use some method parameter without copying it so that the changes are reflected in the calling code.

    The difference to me is that reference notation is much, much, much cleaner and neater to use. But that could just be the C++ side of me talking :-)
    Please rate my post if you felt it was helpful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured