Results 1 to 7 of 7

Thread: Pointer addresses with '&'

  1. #1
    Join Date
    Jan 2012
    Location
    U.K
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Question Pointer addresses with '&'

    Hi,

    I'm currently reading a c++ book, and I'm a bit confused about pointers.

    Here is some of the code which I'm confused about:

    Qt Code:
    1. cout << "Addresses of pointers..." << endl;
    2. cout << "aPtr: " << &aPtr << endl;
    3. cout << "bPtr: " << &bPtr << endl << endl;
    To copy to clipboard, switch view to plain text mode 

    Now, all this does is show me the addresses of 2 pointers, aPtr and bPtr.

    But why do I need the '&'? I tried taking the '&' off and it still works, but the book has the '&' prefix in.

    Thanks for your help.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Pointer addresses with '&'

    Does it show the same values with and without &?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2012
    Location
    U.K
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Pointer addresses with '&'

    Quote Originally Posted by wysota View Post
    Does it show the same values with and without &?
    Hi,

    I'm not too sure if they're the same, but they are both holding a hex address with or without the & operator.

    Thanks,
    - John.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Pointer addresses with '&'

    If every character in two strings is the same then the value is the same. Otherwise (which is probably what you observe) they are different. In fact what & does is that it takes the address of its argument. So if you pass it an object, it will give you the address of the object (i.e. the pointer). If you pass it a pointer to an object, it will return the address of this pointer (i.e. another pointer that points to this pointer).

    See the types below:
    Qt Code:
    1. Object o;
    2. Object *oPtr = &o;
    3. Object **oPtrPtr = &oPtr;
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2012
    Location
    U.K
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Pointer addresses with '&'

    Many thanks for the help.

    But surely:
    Qt Code:
    1. aPointer // a pointer
    2. &aPointer
    To copy to clipboard, switch view to plain text mode 
    Is the same?

    Thanks.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Pointer addresses with '&'

    No. One is e.g. a pointer to object of type X and the other is a pointer to a pointer to an object of type X.

    It's not easy to explain it in a short forum post but in general every variable is just an area in computer's memory that contains a hexadecimal value. So if you have a variable "o" of type "Object" then the object is placed in a memory area starting at some specific memory cell (address). Then you have a variable oPtr which is also a value in the memory. This value is the address of the beginning of memory area where "o" resides. But since oPtr is also a variable and is also placed somewhere in the memory, it also has an address. So oPtrPtr is another variable also positioned somewhere in memory space of your program that contains the value that points to the place where oPtr is.

    So if "o" is positioned in the cell #1, oPtr is positioned in cell #11 and oPtrPtr is positioned in cell #12 then cell #12 will have the value "11" and cell #11 will have the value "1".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2012
    Location
    U.K
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Pointer addresses with '&'

    Aaah I get it now.

    Thank you very much for your help!

Similar Threads

  1. Replies: 0
    Last Post: 21st December 2011, 15:57
  2. Logical Interfaces within Solaris not listed by all addresses
    By umitadelaide in forum Qt Programming
    Replies: 0
    Last Post: 17th October 2011, 10:05
  3. Time taken in getting IP addresses
    By nikhilqt in forum Qt Programming
    Replies: 4
    Last Post: 25th April 2011, 07:22
  4. Replies: 1
    Last Post: 4th December 2010, 17:20
  5. Replies: 1
    Last Post: 8th October 2010, 12:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.