Thread: Call by reference question

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    12

    Call by reference question

    I have a question regarding call by reference - essentially using a function i made and calling it in the main function. I'm passing 2 variables (pointers) into it and the value is copied in the corresponding function but the variable itself is not changed in the calling environment (main).

    ---

    Code:
    void somefunction(arg1, arg2, arg3, arg4, *ans1, *ans2)
    // function does some arithmetic and spits out 2 output values/variables *ans1 and *ans2
    
    ---
    
    NOT SHOWED is the header file that has the function prototype etc
    
    int main(void)
    {
    int = arg1, arg2, arg3, *ans1, **ans11;
    double = arg4, *ans2; **ans22
    
    ans11 = &ans1
    ans22 = &ans2
    
    timediff(arg1, arg2, arg3, arg4, *ans1, *ans2);
    
    printf("answers are %d and %d", ans11, ans22);
    
    return 0;
    }
    ---

    I didn't copy my program in but made a close generic approximation version of it... hopefully that can help a little...

    Thought the best way to do it is try and use a pointer to a pointer...

    Any thoughts?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your question? You might want to show us the smallest and simplest (compilable) program that demonstrates the problem instead of showing remarkably C-like pseudocode.
    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

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can NEVER change anything inside a function that you haven't got the address of. In other words, if what you pass to a function is not a pointer to what you are trying to modify, you will only modify the variable that is local to the function.

    For example:
    pass a variable to modify an integer:
    Code:
    int func(int *p)
    {
       *p = 7;
    }
    
    int main()
    {
    ...
       int x = 3;
       func(&x);
    ...
    }
    Note that the same applies to pointers:
    Code:
    void func2(const char **ptr)
    {
       if (x % 2)   // odd
       {
           *ptr = "Odd";
       }
       else
       {
           *ptr = "Even";
       }
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    12
    Quote Originally Posted by matsp View Post
    You can NEVER change anything inside a function that you haven't got the address of. In other words, if what you pass to a function is not a pointer to what you are trying to modify, you will only modify the variable that is local to the function.

    For example:
    pass a variable to modify an integer:
    Code:
    int func(int *p)
    {
       *p = 7;
    }
    
    int main()
    {
    ...
       int x = 3;
       func(&x);
    ...
    }
    Note that the same applies to pointers:
    Code:
    void func2(const char **ptr)
    {
       if (x % 2)   // odd
       {
           *ptr = "Odd";
       }
       else
       {
           *ptr = "Even";
       }
    }
    --
    Mats
    Thank you.

    Are you saying that I am on the right track with a pointer to a pointer?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by badtwistofate
    Are you saying that I am on the right track with a pointer to a pointer?
    Yes, if your intention is really to change the pointer, not just what it points to.
    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. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. call by value, reference, name...
    By magic101 in forum C Programming
    Replies: 11
    Last Post: 05-06-2008, 03:23 AM
  3. Call by reference help!
    By yigster in forum C Programming
    Replies: 1
    Last Post: 05-02-2008, 09:19 AM
  4. Call by reference...
    By Mak in forum C Programming
    Replies: 4
    Last Post: 11-13-2003, 03:53 PM
  5. function call question?
    By correlcj in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2002, 05:33 PM