Thread: Pointer question

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    22

    Pointer question

    Hi, I'm a bit confused on this pointer question, can anyone shed some light on this?
    Please and thank you

    Code:
    int main()
    {
          int x = 10;
          int *p;
          p = &x;
          cout << p << endl; //should get address of x
          cout << &x << endl; //address of x
          cout << *(&x) << endl; //10
          cout << &p << endl; //<--- this is the address of p
          cout << *(&p) << endl; //address of x , but why is this the address of x??
          system("PAUSE");
          return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    *(&p) == p == &x
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    &p -> get the address of the pointer p, hence making a pointer to the pointer.
    *(&p) -> dereference that pointer to the pointer p, getting back the original pointer p.
    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. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM