CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Does C directly support call by reference?

  1. #1

    Does C directly support call by reference?

    Does C directly support call by reference?

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

    Re: Does C directly support call by reference?

    Not exactly. Passing a pointer is about as close as you can get.

    Reference parameters were added in C++, although most compilers probably just implement it as passing a pointer under the hood anyway.

  3. #3
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Does C directly support call by reference?

    no, C has no concept of references. C uses pointers.

    Code:
    #include <stdio.h>
    
    void foo(int * i){
         *i = 10;
    }
    
    int main(int argc, char** argv){
         int i;
         foo(&i);
         printf("&#37;d", i);
         return 0;
    }

  4. #4
    Join Date
    Mar 2009
    Location
    chennai
    Posts
    7

    Re: Does C directly support call by reference?

    no, Reference parameters can added in C++. Instead of passing Reference, you can pass pointer.

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Does C directly support call by reference?

    The usual way of describing how parameters are passed to a function is call-by-value (a copy is passed) and call-by-reference (a pointer is passed). Since C++ also allow "a third way" using reference parameters I guess this opens for some confusion.

    Anyway as all other already said, C doesn't support parameter passing the C++ reference way.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

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

    Re: Does C directly support call by reference?

    Quote Originally Posted by S_M_A View Post
    The usual way of describing how parameters are passed to a function is call-by-value (a copy is passed) and call-by-reference (a pointer is passed). Since C++ also allow "a third way" using reference parameters I guess this opens for some confusion.

    Anyway as all other already said, C doesn't support parameter passing the C++ reference way.
    The confusion arises when one fails to realize the abstraction, and that while passing a pointer simulates call-by-reference, the pointer itself is still passed by value.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)