Thread: Pointer to a Pointer

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Pointer to a Pointer

    A couple of us are having a small debate over explicitly declaring a pointer to a pointer. This is how I explained it:

    **p can be assigned 'pointer objects'

    whereas

    *p is assigned addresses in memory.



    Is this a valid explaination?




    [edit]I attempted to google this, but as I expected, many tutorials on pointers.. but nothing specifically addressing (pardon the pun) my question.[/edit]
    Last edited by The Brain; 01-01-2005 at 02:20 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Sure.
    It may be a little confusing (it was to me at first) because I read "*p" as a dereference, making it not-a-pointer.

    Explanations with code examples will clear that sort of stuff up.

    gg

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    This is my main argument:

    Code:
    int **pp;
    
    int *p;
    
    int i = 5;
    
    
    p  = &i;       // p now points to i
    
    pp = &p;       // pp now points to p, which points to i
    
    //dereferencing p will result in 5
    cout << *p;
    
    //dereferencing pp will only result in the memory address stored in p
    cout << *pp;     
    
    //double dereferencing pp will result in 5
    cout << **pp;
    Last edited by The Brain; 01-01-2005 at 03:22 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    I don't know if im right or not. Here is an example to test out how I understand a pointer to a pointer:
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int main()
    {
    int i=5;
    int *p=&i;
    int **pp=&p;
    cout<<"&i: "<<&i<<" "<<"&p: "<<&p<<" "<<"&pp: "<<&pp<<endl; 
    cout<<"&i: "<<&i<<" "<<"p: "<<p<<" "<<"pp: "<<pp<<endl;
    cout<<"**pp: "<<**pp<<endl;
      system("PAUSE");
    }
    A Pointer accepts the address of the data type it is. While a Pointer to a Pointer accepts the data type of an int and a pointer.

    Therefore I think of it like this.
    A Pointer such as int *p expects to get the address of an int. While a Pointer to a pointer such as: int** p expects to get the address of an int*. Making the Pointer apart of the datatype it expects.
    And when you dereference a pointer to a pointer. it Deferences the copy of the address in the pointer it points too.
    Help me know if I understand it right or wrong. Thanks

  5. #5
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    >Is this a valid explaination?
    Yes, but it makes a distinction where none exists. A pointer is very simple, it holds an address. T *p holds the address of an object of type T and is called a pointer for convenience. T **p holds the address of a pointer to an object of type T. The same logic holds for however many levels of indirection you want: T *****p holds the address of a pointer to a pointer to a pointer to a pointer to T.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    Ok I think I got a better explaination of a pointer to a pointer. I will not give up. A Pointer expects type Datatype* and a pointer to a pointer Expects type datatype**. the First is pointing to just an address. and the pointer to a pointer points to the address of another address. It uses the Address of the pointer to point to. but Deferences the address within the Pointer.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    New approached to explainations
    A pointer to a pointer Is a Variable that uses two layers of Indirection while a Pointer just uses one Layer of Indirection
    The Number of * shows how many indirections has must be used.

  8. #8
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    hmm... I prefferd RoDs d:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM