Thread: Returning Address with Return Statement?

  1. #1
    Registered User Waleed Mujeeb's Avatar
    Join Date
    Jan 2012
    Posts
    22

    Returning Address with Return Statement?

    Code:
    class array
    {
     int arr[3];
     public:
     int &access(int n)
     {
    
    
      return arr[n];
     }       
          };
    int main()
    {
    array a;
    for(int i=0;i<3;i++)
    cin>>a.access(i);
    for(int i=0;i<3;i++)
    cout<<a.access(i)<<endl;
    cin.get();
    }
    My question is "Is return arr[n] returning the address of arr[n]?",If so then why does the cout print the values rather than address?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Waleed Mujeeb
    Is return arr[n] returning the address of arr[n]?
    No, it returns a reference to arr[n] since the return type of the function is int&.
    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 Waleed Mujeeb's Avatar
    Join Date
    Jan 2012
    Posts
    22
    If that is the case then why does scanf("%d",a.access(i)); not work and scanf("%d",&a.access(i)); works ?
    Last edited by Waleed Mujeeb; 03-23-2012 at 01:33 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Waleed Mujeeb
    If that is the case then why does scanf("%d",a.access(i)); not work and scanf("%d",&a.access(i)); ?
    Because scanf expects an int* argument to correspond to the %d format specifier.

    Are you aware of the differences between C++ references and pointers?
    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

  5. #5
    Registered User Waleed Mujeeb's Avatar
    Join Date
    Jan 2012
    Posts
    22
    Thanks, I messed up addressess and References. Need to check em out.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Btw, avoid scanf in C++. Use std::cin.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Address off when returning pointer from function.
    By dtow1 in forum C Programming
    Replies: 3
    Last Post: 09-14-2011, 02:11 PM
  2. Replies: 5
    Last Post: 06-30-2008, 02:48 PM
  3. Returning the address of a member structure
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 05-03-2006, 12:51 AM
  4. Returning address to temp var.
    By Calthun in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2004, 01:42 PM
  5. Returning address of pixel...
    By Cheeze-It in forum Game Programming
    Replies: 14
    Last Post: 08-27-2001, 05:57 PM