Any Uses For A Pointer To A Pointer?

This is a discussion on Any Uses For A Pointer To A Pointer? within the C++ Programming forums, part of the General Programming Boards category; I never really messed with learning much about pointers to pointers or their uses....nor did I really ever see a ...

  1. #1
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    Any Uses For A Pointer To A Pointer?

    I never really messed with learning much about pointers to pointers or their uses....nor did I really ever see a use for pointers to function addresses either.

    Pointers/memory management are the hardest concepts of C and C++, hands down. There are so many different uses and syntax for them. I never really saw a need to have a pointer to a pointer....what's the use? I assume it's useful to produce a tree effect with your pointers, and the **p would be like a root node....or I could be wrong. I also heard that **p is useful in keeping collections of const char *strings.

    At any rate, pointers never were my strong point, but I'd like to master them so I can put the worst of C/C++ behind me. A lot of code for advanced applications or in APIs is very pointer-intensive. Not that I don't understand that particular code....but I'm just not yet a master of everything pointer.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    I had a problem the other day, one of the solutions involves using a pointer to a pointer.
    Problem with Dynamically Increasing Array of Integers
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    You can use a pointer to a pointer to allocate two dimensional arrays.
    Code:
    int **buf, rows = 10, cols = 10;
    buf = new int*[rows];
    for(int i = 0; i < rows; i++)
    	buf[rows] = new int[cols];
    You can use a pointer to a pointer if you need to pass a pointer by reference. (Alternately, you could just use a reference, but C doesn't have that.)
    Code:
    void func(int **a) //double pointer, valid C code
    {
    	*a = (int*)1;
    }
    
    void func(int *&a) //reference to a pointer, invalid C code
    {
    	a = (int*)2;
    }
    
    int main()
    {
    	int *a = NULL;
    	func(&a); //pass the address of the pointer
    	if(a == (int*)1)
    		std::cout << "1" << std::endl;
    	func(a); //pass the pointer
    	if(a == (int*)2)
    		std::cout << "2" << std::endl;
    	return 0;
    }
    Just like any tool, they have their occasional uses. I prefer to use STL to take care of that memory management stuff.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I had a problem the other day, one of the solutions involves using a pointer to a pointer.
    And you can also use a reference to a pointer.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I assume it's useful to produce a tree effect with your pointers, and the **p would be like a root node....

    For a binary tree, you could have something like:
    Code:
    struct Node
    {
       int data;
       Node *left;
       Node *right;
    };
    
    Node *root;
    When updating the tree, you again could pass a reference to a node:
    Code:
    void addNode(Node *&root)
    Or even:
    Code:
    void addNode(Node &root)
    though that isn't as efficient.

    In C, since there are no references, you would need the double pointer:
    Code:
    void addNode(Node **root)
    Or I guess you could return the root:
    Code:
    Node *addNode(Node *root)

  6. #6
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    About the double pointer as a reference thing...I suppose I get it...I'm not really sure if I'd ever need to pass a pointer to a function to manipulate its contents...but I get it....if you have a pointer as a parameter, it expects to receive an address, ya?

    I'm still not so great with const char *Strings. Maybe I just really suck at strings. Would there ever be a time when you'd want to use a double char pointer?

    These confuse me the most:

    Code:
    #include <iostream>
    using namespace std;
    
    int main(){
        char *S = "Hello sir.";
        cout << S << endl;       //Shouldn't this print an address?
        cout << *S << endl;     //Why does this only display the first slot of the array?
        cin.get();
        return 0;
    }
    Last edited by Krak; 06-02-2005 at 10:26 PM.

  7. #7
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Krak
    About the double pointer as a reference thing...I suppose I get it...I'm not really sure if I'd ever need to pass a pointer to a function to manipulate its contents...but I get it....if you have a pointer as a parameter, it expects to receive an address, ya?

    I'm still not so great with const char *Strings. Maybe I just really suck at strings. Would there ever be a time when you'd want to use a double char pointer?

    These confuse me the most:

    Code:
    #include <iostream>
    using namespace std;
    
    int main(){
        char *S = "Hello sir.";
        cout << S << endl;       //Shouldn't this print an address?
        cout << *S << endl;     //Why does this only display the first slot of the array?
        cin.get();
        return 0;
    }
    After this
    char *S = "Hello sir.";
    S will contain a pointer, which is a memory adress of string "Hello sir". ostream has an overloaded operator for const char* and char*, which assumes that that's a C style string.
    the ' * ' unary operator in *S is to dereference the pointer, or in other words, since you have a pointer to something, * gets that something. In your case *S will give you the first char pointer by S, which is a simple char. ostream presents that single char.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    cout << "enter the number of strings you want to save" << endl;
    int x;
    cout << "enter the maximum number of letters per string you want to allow" << endl;
    int y;
    char ** strings; //pointer to pointer to type char
    strings = new char*[x]; //declare number of arrays to use using user input
    for(int i = 0; i < x; ++i)  
      strings[i] = new char[y + 1]; //declare max length of each string
    for(int i = 0; i < x; ++i)
    {
      cout << "enter a string with less than " << y << letters << endl;
      cin >> strings[i];
    }
    an example of an array of C style strings allowing user to specify number of strings and size of maximum length of string. Code is not supposed to be optimal code, but meant for demonstration purposes only.

    Or how about a guessing game where player tries to remember where certain letters are located and if they can pair them up then they gain a point and the player with the most points when all letters have been paired wins. If you wanted to let user decide the number of cells used in the game, then you could could use a dynamically declared 2D array of char, not even strings, to represent the playing board, etc.
    Last edited by elad; 06-03-2005 at 10:51 AM.
    You're only born perfect.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    It sounds like Java or C# would be the perfect language for you: no pointers, no deleting, no worrying about memory leaks.

  10. #10
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    The Win32 SDK uses pointers to pointers quite a bit (called handles). The explanation for their use in that situation is that when the computer is dealing with lots of Window-esque objects, it often needs to move their data around in memory. If you were just using a pointer to this data and it was moving constantly (i.e. the value of the pointer was changing constantly) you'd very quickly run into problems. If you have a constant pointer to that changing pointer, it's a lot easier to manage.

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Is that what a handle is----a pointer to pointer to type. Whaddya know.
    You're only born perfect.

  12. #12
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Unless I grossly misunderstood Folger's tutorial - then yes.

  13. #13
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    It makes sense. It gives you a handle to hold on to and keep track of the other little pointer while he runs around, ya?

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is that what a handle is----a pointer to pointer to type.
    Well, technically a pointer is a handle. A pointer to a pointer is also a handle because a pointer (even though it's a handle) is also valid content.

    >I never really messed with learning much about pointers to pointers or their uses
    In C++ their use is limited because of references. I rarely use a pointer to a pointer in C++ where in C I found myself using three or four levels of indirection occasionally.

    >nor did I really ever see a use for pointers to function addresses either
    Once again, their use is limited thanks to function objects.
    My best code is written with the delete key.

  15. #15
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Stroustrup defines a handle much like a smart pointer. Probably a lot of variant meaning. The winapi handles are an opaque type. I think they're just unsigned integers into some internal winapi datastructure. They behave like pointers, but are not really pointers.
    Last edited by okinrus; 06-04-2005 at 09:25 AM.

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

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