Thread: Pass by value/reference

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    140

    Pass by value/reference

    Hi

    In the following example, is the variable integer being passed by reference or by value?

    Code:
    void test(int* var) {
        *var++;
    }
    
    int main() {
        int integer = 1;
        test(&integer);
        printf("%d", integer);
        return 0;
    }
    The reason why I ask is because according to Pass By Value - Definition, pass by reference is not possible in C (since there are no references).

    Best,
    Niles.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The variable named integer is not passed at all. Rather, the address of the variable named integer is passed by value. This is used to simulate passing the variable named integer by reference.
    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
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by Niels_M View Post
    according to Pass By Value - Definition, pass by reference is not possible in C
    This is true - all function arguments are passed by value. However, the variable integer is not what's being passed - what's being passed is the address of the variable "integer". This address is indeed being passed by value, so you could say it's "simulating" pass-by-reference.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Hi. I dont understand why.
    according to Pass By Value - Definition, pass by reference is not possible in C
    I thought variables could be either passed by Value/Reference?

    Reference - Taking reference from original variable's address
    Value - Taking concurrent value of particular variable

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Inneart
    I thought variables could be either passed by Value/Reference?

    Reference - Taking reference from original variable's address
    Value - Taking concurrent value of particular variable
    Yes, and then there are other parameter passing schemes that are possible. However, that does not mean that all those parameter passing schemes are directly available in every programming language.
    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

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's really misnamed. It's not pass-by-reference. It's pass-by-address. The address of the variable is copied into the function.
    Pass-by-value copies the variable value into the function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    So in C language, variables are always passed by duplicating its value?

    C = 5
    Pass C to a F(x)
    C of F(x) duplicates value of C

    And if I want it to modify the original C,

    C = 5
    Pass &C to a F(x)
    C of F(x) chops address of original C

    Is that right?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Chops off?
    The last example passes the address of C to F(x), allowing F(x) to write to the address instead of the local C to reflect the changes to the caller.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Chop as in take.

    So my examples are correcto?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In that case, yes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by laserlight View Post
    The variable named integer is not passed at all. Rather, the address of the variable named integer is passed by value. This is used to simulate passing the variable named integer by reference.
    So pass by reference is just the adress being passed by value? So it is OK so say that C has pass by reference as well?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Pass by reference is a concept and not a language feature. So in a sense, it is OK to do that.
    I think it's silly, though. Clearly it isn't passing by reference, since the address is passed.
    Furthermore, C++ also has a pass-by-reference language feature.

    Conclusion: whether it is acceptable or not is up to you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by Elysia View Post
    Pass by reference is a concept and not a language feature. So in a sense, it is OK to do that.
    I think it's silly, though. Clearly it isn't passing by reference, since the address is passed.
    Furthermore, C++ also has a pass-by-reference language feature.

    Conclusion: whether it is acceptable or not is up to you.
    So what happens when we in C++ have pass-by-reference (I know how to pass a variable by reference, but what actually happens)? Does the address get copied or what?

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's pass-by-reference. The new variable in the function is an alias of the one you passed in. That is what the standard says.
    In practice, it is usually implemented using pointers. But again, it doesn't have to be.
    Thus, it is really a "true" pass-by-reference feature.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Niels_M View Post
    So pass by reference is just the adress being passed by value? So it is OK so say that C has pass by reference as well?
    Basically that's all that pass by reference is in any language... you're giving the address of the variable instead of the value of the variable. C is just more honest about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  2. Replies: 3
    Last Post: 11-22-2007, 12:58 AM
  3. Pass by reference
    By mcgeady in forum C Programming
    Replies: 11
    Last Post: 02-17-2005, 03:01 AM
  4. pass be reference versus pass by value
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 01:03 PM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM