Thread: passed by reference or pointer ?

  1. #1
    Registered User vohu's Avatar
    Join Date
    Feb 2012
    Posts
    6

    passed by reference or pointer ?

    Hello,

    I have a question : in C language, can we pass value by reference, as we do in C++ language ?

    For example :
    I know the method with pointers :
    int myfunction (int *var) //like that

    and, I saw in C++, we can use the method by reference with & :
    int myfunction (int &var)

    there is the same thing in C language, isn't it ?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I have a question : in C language, can we pass value by reference, as we do in C++ language ?
    No pass by reference is only valid in C++.

    Jim

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    No, C doesn't have references, you have to pass pointers.
    Btw, you've got C++ references wrong too.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by manasij7479 View Post
    Btw, you've got C++ references wrong too.
    How? The only thing mentioned was the use of an ampersand.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Subsonics View Post
    How? The only thing mentioned was the use of an ampersand.
    My mistake, I did not see that it was a declaration and automatically thought it was a call by the absence of a semicolon.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    In C you simulate pass by reference with pointers.
    Code:
    void func(int * const n) {
        *n = 42;
    }
    // elsewhere
    int x = 41;
    func(&x);

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by oogabooga View Post
    In C you simulate pass by reference with pointers.
    Code:
    void func(int * const n) {
        *n = 42;
    }
    // elsewhere
    int x = 41;
    func(&x);
    I wouldn't call that a very convincing simulation, since pointers are available in C++ as well. The difference is that you have to remember the level of indirection and dereference accordingly. References in C++ is more like an alias, so the reference can be treated as if it is what it references. I'm not saying this because I think you aren't aware of it btw, just to say that IMO they are a different thing.
    Last edited by Subsonics; 02-23-2012 at 10:20 AM.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by vohu View Post
    Hello,

    I have a question : in C language, can we pass value by reference, as we do in C++ language ?
    Formally no, because C has pointers -- most languages that use pointers do not use references and vice versa.

    Pointers provide more functionality than references but are slightly more complicated to use, which is why C++ incorporated references.

    Code:
    // with pointers
    void myfunc (int *x);  // prototype
    int n;
    myfunc(&n);   // call
    
    // with references
    void myfunc (int &x);  // prototype
    int n;
    myfunct(n);
    The second style is simpler and less prone to error. Because pointers allow you to do things you cannot do with a reference (such as arithmetic with memory addresses), where this functionality is undesirable, a reference would be considered "safer".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by MK27 View Post
    Because pointers allow you to do things you cannot do with a reference (such as arithmetic with memory addresses), where this functionality is undesirable, a reference would be considered "safer".
    One can always grab the address of the referenced variable..

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by oogabooga View Post
    In C you simulate pass by reference with pointers.
    It might be better to think of it the other way around: a reference simulates one common use for a pointer. "Pass by reference" is simply a conceptualization expressing this use-value.

    One can always grab the address of the referenced variable..
    Right, then you have a pointer. The point of the reference is abstraction. This is generally considered a good thing, which is why most languages which employ references don't provide such a mechanism (I think C++ is probably unique here).
    Last edited by MK27; 02-23-2012 at 10:42 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    It might be better to think of it the other way around: a reference simulates one common use for a pointer. "Pass by reference" is simply a conceptualization expressing this use-value.
    Yeah, if you're coming from an understanding of C++ references (admittedly likely to be the case for vohu), otherwise "a reference simulates one common use for a pointer" does not make sense since pointers are "references" in a computer science perspective: "pass by reference" or "call by reference" is a parameter passing method in computer science that is implemented in C++ by reference parameters and simulated in C and C++ by pointer parameters. Hence we talk about "dereferencing a pointer" to obtain what the pointer 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

  12. #12
    Registered User vohu's Avatar
    Join Date
    Feb 2012
    Posts
    6
    Finaly, the reference simplify the use of pointers ?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vohu
    Finaly, the reference simplify the use of pointers ?
    In C++, yes, you can look at it in that way.
    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

  14. #14
    Registered User vohu's Avatar
    Join Date
    Feb 2012
    Posts
    6
    and, the expression "pass by reference" in C langage is a malapropism...I read that php language use automatic passing reference, this functionnality is integrated in C++11 ?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vohu
    the expression "pass by reference" in C langage is a malapropism.
    It depends on your point of view. As I said, a pointer is a reference. I agree with saying that call by reference is "simulated" when passing a pointer because an explicit dereference is required, otherwise all that happens is that the pointer is passed by value; nonetheless the pointer refers to what it points to.

    Quote Originally Posted by vohu
    I read that php language use automatic passing reference
    No, unless otherwise specified, arguments in PHP are passed by value. However, since PHP5, an object variable actually contains a reference to the object, not the object itself, i.e., it is an object reference, so you can see it as a pointer without pointer syntax.

    Quote Originally Posted by vohu
    this functionnality is integrated in C++11 ?
    No, in C++, an object variable really does contain an object.
    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. string pointer passed to a function help
    By BrandNew in forum C Programming
    Replies: 7
    Last Post: 03-07-2011, 11:08 AM
  2. Dynamic Array Passed to a function by Reference
    By patriots21 in forum C Programming
    Replies: 7
    Last Post: 11-21-2010, 11:32 AM
  3. help undestanding what pointer is being passed
    By reakinator in forum C Programming
    Replies: 1
    Last Post: 05-10-2008, 12:06 AM
  4. problem with variable passed as reference
    By crash88 in forum C Programming
    Replies: 1
    Last Post: 05-16-2006, 07:16 PM
  5. Length of string changes when passed with pointer
    By BungleSpice in forum C Programming
    Replies: 3
    Last Post: 03-21-2004, 12:52 PM