Thread: Return by reference

  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,897
    >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,817
    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,897
    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,130
    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, 11:45 AM
  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, 08:36 PM
  3. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  4. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM
  5. string class errors
    By CodeMonkey in forum C++ Programming
    Replies: 13
    Last Post: 07-20-2003, 11:20 PM