Returning Address with Return Statement?

This is a discussion on Returning Address with Return Statement? within the C++ Programming forums, part of the General Programming Boards category; Code: class array { int arr[3]; public: int &access(int n) { return arr[n]; } }; int main() { array a; ...

  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
    21,313
    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&.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,313
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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

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