Like Tree1Likes
  • 1 Post By laserlight

Address of a C++ reference

This is a discussion on Address of a C++ reference within the C++ Programming forums, part of the General Programming Boards category; I was told that in c++ the address of a data type and the address of its reference are same. ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    384

    Address of a C++ reference

    I was told that in c++ the address of a data type and the address of its reference are same. But when I compiled and executed the following code in Microsoft Visual C++ 6.0, it printed two seperate addresses for i and j. Any ideas, why??

    Code:
    #include<iostream.h>
    void main()
    {
    	const int i=10;
    	const int &j=i;
    	
            cout<<"\nAddress of i="<<&i<<"\nAdress of j="<<&j<<endl;
    }
    Last edited by juice; 09-26-2011 at 10:40 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    * Thread moved to C++ programming forum. *

    Quote Originally Posted by juice
    I was told that in c++ the address of a data type and the address of its reference are same. But when I compiled and executed the following code in Microsoft Visual C++ 6.0, it printed two seperate addresses for i and j. Any ideas, why??
    Maybe it is a bug with MSVC6.

    By the way:
    • <iostream.h> should be <iostream>
    • void main should be int main
    • In order to use the names cout and endl without qualifying them, you should use the appropriate using directive (e.g., using namespace std; ) or using declarations (e.g., using std::cout; ).
    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
    Join Date
    Aug 2011
    Posts
    384
    Actually different addresses are reported when the variables are declared 'constant'.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by juice View Post
    Actually different addresses are reported when the variables are declared 'constant'.
    Probably still a bug; VS 2010 gives the correct answer.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote Originally Posted by juice
    Actually different addresses are reported when the variables are declared 'constant'.
    That should not make a difference. Since j is an alias of i, the address of j is the address of i.
    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

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Hey, I tried to build it with my MSVC 2010, and it flags an error:
    "Cannot open include file: 'iostream.h': No such file or directory"

    Any ideas why?? I am new to this compiler.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote Originally Posted by juice
    Hey, I tried to build it with my MSVC 2010, and it flags an error:
    "Cannot open include file: 'iostream.h': No such file or directory"

    Any ideas why?? I am new to this compiler.
    Refer to my corrections in post #2.
    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

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Yeah, its a bug in MSVC6.

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Thanx laserlight.
    Can you explain me why <iostream.h> should be <iostream>..

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote Originally Posted by juice
    Can you explain me why <iostream.h> should be <iostream>
    Yes. <iostream> is the correct standard header.
    iMalc likes this.
    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

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by juice View Post
    Thanx laserlight.
    Can you explain me why <iostream.h> should be <iostream>..
    <iostream.h> is not a standard header, wheras <iostream> is.
    In some long ages past, iostream.h may have been part of C++, but it certainly isn't as of today.

    Also, there is no reference to speak of in this thread. All you are doing is taking the address of a variable, hence creating a pointer. But a pointer is not a reference. Separate the concepts.
    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.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote Originally Posted by Elysia
    Also, there is no reference to speak of in this thread.
    That is not correct. In the code posted in post #1, j is a reference.
    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

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    You are right. My bad.
    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. Replies: 1
    Last Post: 11-07-2010, 10:39 PM
  2. C De-reference + Memory address together in struct?
    By valaris in forum C Programming
    Replies: 6
    Last Post: 06-18-2008, 01:31 AM
  3. Block address from word address
    By xddxogm3 in forum Tech Board
    Replies: 0
    Last Post: 04-25-2007, 09:02 PM
  4. Call by Reference/Address Errors - Please Help
    By Vireyda in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2004, 05:50 PM
  5. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 12:25 AM

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