Return by reference

This is a discussion on Return by reference within the C Programming forums, part of the General Programming Boards category; Can I run this code in C .I have successfully run it in C++ but it gives me trouble in ...

  1. #1
    tucker
    Guest

    Return by reference

    Can I run this code in C .I have successfully run it in C++ but it gives me trouble in C
    int & At()
    {
    int x;
    return x;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Can I run this code in C
    No, C doesn't support references

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    You will need to use pointers, but even then the code will not work because the value that you are returning is that of a local variable that will go out of scope once the function exits. You would either want to pass a pointer to the variable you will be changing into the function, make the variable a global, or make the variable declaration within the function "static".
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Unregistered
    Guest

    Return by reference

    what's the problem with this code in "C"
    int& f()
    {
    int x;
    return x;

    }

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    int& f() /* Returns a reference, invalid C */
    { 
      int x; 
      return x; /* Returns a local variable, incorrect C */
    }
    Code:
    /* This is correct, but has a potential bug */
    int *f ( void )
    {
      static int x;
      return &x;
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    As already discussed,

    int& f()
    {
    int x;
    return x;
    }

    is incorrect C, because there don't exist references ( int& = reference to int ) in C, only in C++.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

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, 12:45 PM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  3. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 05:42 PM
  4. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 11:40 AM
  5. string class errors
    By CodeMonkey in forum C++ Programming
    Replies: 13
    Last Post: 07-21-2003, 12:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21