Thread: Address of a C++ reference

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

    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
    28,413
    * 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; ).
    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
    Join Date
    Aug 2011
    Posts
    385
    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,949
    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
    28,413
    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.
    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

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    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
    28,413
    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.
    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

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

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    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
    28,413
    Quote Originally Posted by juice
    Can you explain me why <iostream.h> should be <iostream>
    Yes. <iostream> is the correct standard header.
    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

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    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.
    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

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 11: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, 06:50 PM
  5. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 01:25 AM