CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: just curious

  1. #1
    Join Date
    Mar 2005
    Posts
    55

    Lightbulb just curious

    Hi,

    In this code, the output of the 2nd cout is just the address of v1. What is the output of the 3rd cout? Does it mean anything?

    main()
    {

    int *p1, v1;

    p1 = &v1;
    v1 = 20;
    cout << *p1 << endl;
    cout << p1 << endl;
    cout << &p1 << endl;
    return 0;
    }

    Thanks.
    Manoj

  2. #2
    Join Date
    Jan 2004
    Location
    Baltimore, MD
    Posts
    76

    Re: just curious

    It's the address of the memory that stores the address of v1.
    "Get ready, cuz this ain't funny. My name is Mike D and I'm about to get money."

  3. #3
    Join Date
    Mar 2005
    Posts
    55

    Re: just curious

    Ok, output 2nd gives the address of the memory that store the v1. But the output 2nd and 3rd are different.

  4. #4
    Join Date
    Jan 2004
    Location
    Baltimore, MD
    Posts
    76

    Re: just curious

    Yes, they are different. When you create int v1, it allocates a block of memory that can hold an integer. When you create int *p1, it allocates a block of memory that can hold the address of an integer (the address is a numerical value). When you output p1, it tells you the address of the block of memory that holds the value of v1. When you output &p1, it tells you the address of the block of memory that holds the address of v1.
    Last edited by dro873; March 11th, 2005 at 11:32 PM.
    "Get ready, cuz this ain't funny. My name is Mike D and I'm about to get money."

  5. #5
    Join Date
    Mar 2005
    Posts
    55

    Thumbs up Re: just curious

    Thanks dro873.

  6. #6
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: just curious

    It doesn't matter in fact what does the p1 pointer store: &p1 would be the same. It's just the address of object p1, which is a pointer and can store an address of another object. And no matter what it stores it is located at the same address, so &p1 would be also the same.
    "Programs must be written for people to read, and only incidentally for machines to execute."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured