Thread: functions - pass by reference - pass by pointer - increment++ issue

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    44

    functions - pass by reference - pass by pointer - increment++ issue

    I decided to learn a little more about passing values to functions via a pretty thorough C++ tutorial. So, using the onlinecompiler.net compiler, I wrote a simple program to call two identical functions. The difference between the 2 is one uses pass by reference, one pass by address.

    I have already used this construct in my first (almost complete) C program.

    int func (TCHAR* st1, TCHAR* st2, TCHAR* st3, int *start, int *end)

    *start = somelocallydefinedint;

    -----------------------------

    main ()

    int start, end;
    x = func (st1, st2, st3, &start, &end);

    ----------------------------

    During my test of today, I found that:

    *start++;

    does not work as expected. Seems I need:

    (*start)++;

    Which is ironic in that I have one function in my current program that receives a pointer to a WIN32_FIND_DATA struct and in that, the (*struct).fieldinsidestruct syntax must be used.

    So, the other way:

    int func (........, int &start, int &end)

    seems to have the advantage in that, within the function, I don't have to use pesky asterisks and I can also do a start++ with no issues.

    The question then becomes: for integers, is there any reason to use the first example of * references vs. the latter, seemingly more concise, & references?

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    (*struct).fieldinsidestruct is equivalent to the short-form struct->fieldinsidestruct
    Yes you need to use parenthesis for (*start)++ because otherwise postfix increment happens too early.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jlewand
    The question then becomes: for integers, is there any reason to use the first example of * references vs. the latter, seemingly more concise, & references?
    The most important reason is that C does not have C++-style references

    If you were programming in C++ instead, then passing a pointer would still make sense if the parameter were optional (in which case a null pointer could be passed).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    Quote Originally Posted by jlewand View Post
    The question then becomes: for integers, is there any reason to use the first example of * references vs. the latter, seemingly more concise, & references?
    I consider it a bug to declare a function parameter (in C++ obviously) as a reference (with &) if the value is modified in the function. The problem is, you can't tell by looking at the function call if it is pass by reference or pass by value--you have to go back and look at the function or its prototype or just remember. And there are far too many things to remember already.

    I've taken to commenting other people's code which I chance to touch, and my own code if I have to call such a function. I make the call look like this:
    Code:
    int func(int& var);      // Declared somewhere like this
    
    x = func(/*&ref*/var);   // My comment draws attention to the gotcha

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by KenJackson
    I consider it a bug to declare a function parameter (in C++ obviously) as a reference (with &) if the value is modified in the function. The problem is, you can't tell by looking at the function call if it is pass by reference or pass by value--you have to go back and look at the function or its prototype or just remember. And there are far too many things to remember already.
    This is a weakness of non-const references, but I don't consider it a bug: I prefer to have a required out parameter be a reference than a pointer, otherwise the check for a null pointer has to be performed or there is the risk of undefined behaviour, and the latter would be a genuine bug.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass by Reference with a pointer to char
    By JoshNCSU22 in forum C Programming
    Replies: 16
    Last Post: 10-10-2008, 11:28 AM
  2. Pass by reference vs pass by pointer
    By Non@pp in forum C++ Programming
    Replies: 10
    Last Post: 02-21-2006, 01:06 PM
  3. pass by reference .... HELP!!
    By NamelessNoob in forum C++ Programming
    Replies: 19
    Last Post: 02-15-2006, 12:50 PM
  4. Pass by pointer, reference, value.
    By Lithorien in forum C++ Programming
    Replies: 8
    Last Post: 02-25-2005, 10:03 AM
  5. pass be reference versus pass by value
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 01:03 PM