Pointers and their Addresses

This is a discussion on Pointers and their Addresses within the C++ Programming forums, part of the General Programming Boards category; /* ok my question is the following let's say you declare a variable and itialize it.....and then you assign the ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Pointers and their Addresses

    /* ok my question is the following let's say you declare a variable and itialize it.....and then you assign the address of that variable to a pointer, wouldn't the address of the pointer then be the same as the address of the variable that was created?Cosider the following:*/


    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    int main()

    {
    int yourAge=15; //yourAge is initialized
    cout<<"The address of yourAge is "<<&yourAge<<endl;
    int *yourAgeManipulation=&yourAge; /*pointer is created and assigned your Age's
    address */

    cout<<"The addres of *yourAgeManipulation is"<<&yourAgeManipulation<<endl;/*Shouldn't *yourAgeManipulation have the same address as that of youAge. If it should tell me why if not please tell me why as well.*/

    system("PAUSE");
    return 0;
    }

    /*#####################################*/
    OUTPUT:
    The address of yourAge is 0x254fdd4
    The addres of *yourAgeManipulation is0x254fdd0
    Press any key to continue . . .

    although the two address do look pretty close........please help
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    Unregistered
    Guest
    No they will not. The pointer is a seperate entity and therefore has its own memory address. However, since it is a pointer, it will mimic whatever is in the initial memory address that it was set to.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Change

    cout<<"The addres of *yourAgeManipulation is"<<&yourAgeManipulation<<endl;

    to

    cout<<"The addres of *yourAgeManipulation is"<<yourAgeManipulation<<endl;

    And you will get a more expected result

    Notice how the differences between the memory addresses is the same size as an int

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Law of Physics #1:
    "Two Bodies Cannot Occupy The Same Space at Once"

    This is true of pointers too. A pointer has it's own location in memory, and will never reside at the same address as any other object...

    Data........../..Address../..Value
    -------------------------------------
    int num....../..&1000.../..699
    int *pnum../..&2000.../..&1000

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    In C++ the alias &reference operator, however, does create an object which basically IS the referenced object(same memory location).

    int num = 10;

    int &nic = num;

    nic = 15;

    //...now num = 15

    Unlike the pointer, an alias must cannot point to any other object , and must be initialized at the time of creation.
    Last edited by Sebastiani; 12-29-2001 at 06:12 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Yeah I guess they can't occupy the same space that's true....I think I get it know though the value of *yourAgeManipulation is the address of your age.

    Please tell me if I am right or not.
    Last edited by incognito; 12-29-2001 at 06:09 PM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Wink

    Exactly.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

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