Pointer question.

This is a discussion on Pointer question. within the C++ Programming forums, part of the General Programming Boards category; Well, pointers are a big thing in C++, I understand them for the most part, but I'm lost on one ...

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Pointer question.

    Well, pointers are a big thing in C++, I understand them for the most part, but I'm lost on one thing:
    Can you pass a pointer adress?

    Like:

    Code:
    int a;
    int *b;
    
    b=a;
    
    cout << &b;
    If I'm not mistaken, that should return a's adress right? But then, how are pointers saved to memory if they have an adress that is exactly the same? That would confuse the computer. The only logical way I can see pointers working is:

    Code:
    int a=0;
    int *b;
    
    b=a; //Copy the contents of a to b's adress.
    
    b++; //Add one to b, and add one to a.
    
    a=2; //Make a equal 2.
    b ends up 1, a ends up 2. So am I correct in thinking that pointers are merely duplicates of a number, and, when changes, change both the "pointer" value and the actual value?

    Or, am I off with this theory?
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    If I'm not mistaken, that should return a's adress right?
    No. b is NOT a pointer to a but merly contains an address whose value is a. If you want to make a pointer to a, then do this:
    Code:
    b = &a;
    b++; //Add one to b, and add one to a.
    That only increments b by sizeof(b) bytes, does nothing at all to a.

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Code:
    b = &a;
    b++;
    That would increment 'a' by one right?

    And anyways, the code isint what I'm getting at. I'm wondering what this does:

    Code:
    b = &a;
    cout << &b; //This should return a's adress ... right?
    And above, I listed why that doesnt sound plausable... Or does the pointer actualy switch adresses with 'a'?
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    &b produces the address of b, not a. Try this and you will see the difference.
    Code:
    	int a = 0;
    	int* b = &a;
    	cout << &b << endl;
    	cout << b << endl;

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by Ancient Dragon
    Code:
    b++; //Add one to b, and add one to a.
    That only increments b by sizeof(b) bytes, does nothing at all to a.
    sizeof(*b)
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

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, 09:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 10:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 01:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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