446,191 Members | 751 Online
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

# Pointer to Pointer

 P: 16 Hi guyz, could you please tell me what's the use of pointer to a pointer? specially in function parameters. Regards, Feb 25 '07 #1
7 Replies

 Expert 2.5K+ P: 3,652 A pointer to a pointer is sometimes used to make dynamically-allocated 2D arrays. For instance, Expand|Select|Wrap|Line Numbers int **array; array = new int*[10]; for (int i = 0; i < 10; i++) {    array[i] = new int[10]; } This code will make a 10x10 array of integers. Functions that accept double pointers (what I call them - not an official term) can use them as 2d arrays in the above manner. I'm not sure if there's any other widespread usage of double pointers... Feb 26 '07 #2

 P: 16 A pointer to a pointer is sometimes used to make dynamically-allocated 2D arrays. For instance, Expand|Select|Wrap|Line Numbers int **array; array = new int*[10]; for (int i = 0; i < 10; i++) {    array[i] = new int[10]; } This code will make a 10x10 array of integers. Functions that accept double pointers (what I call them - not an official term) can use them as 2d arrays in the above manner. I'm not sure if there's any other widespread usage of double pointers... tnx for the reply Feb 26 '07 #3

 Expert Mod 5K+ P: 8,916 I'm not sure if there's any other widespread usage of double pointers... Loads :D You can use a pointer to pointer in a function call if you want to return a pointer via a parameter, i.e. Expand|Select|Wrap|Line Numbers char *pp;   Function(&pp);     void Function(char **ppp) {     (*ppp) = malloc(20);       strcpy(*ppp, "Hellow World"); }   You need a pointer to a pointer to pass back the value of a pointer (in the same way you need a pointer to an int to pass back the value of an int), if you just had a pointer then the value of pp would not be changed. Pointers to pointers can be useful in linked list manipulation, sometimes it is handy to have a pointer to your next pointer. Just allocating memory is a rather trivial and boring use of a pointer to pointer that doesn't really exploit it's full potential. Feb 26 '07 #4

 100+ P: 1,806 Furthermore (and forgive me if i'm repeating what you saud Banfa) a double pointer allows the called function to worry about memory...... Consider two functions: Expand|Select|Wrap|Line Numbers   int myMethod(char *inString, int size); int myOtherMethod(char **inString, int *size);   myMethod can manipulate the data starting at pointer inString, but has to assume that memory has been allocated (generally you would expect that this is why size has been passed also). Expand|Select|Wrap|Line Numbers int myMethod(char *inString, int size) {   char* tempString = inString;   for(int i=0; i 0)  //Notice value AT size   {     tempBuffer = (char *)malloc(*size);   }   else   {     tempBuffer = (char *)malloc(DEFAULT_SIZE);     *size = DEFAULT_SIZE;   }   //Do whatever we need to do with data     //point callers pointer to our buffer   *inString = tempBuffer; }   NOTICE: we say *inString = tempBuffer ("the value AT inString = tempBuffer", since tempBuffer is a ptr and inString is a ptr to a ptr we update the (ptr) value AT ptr, ptr ) } Feb 26 '07 #5

 Expert 5K+ P: 5,000 However, be warned that the procedure that DeMan is talking about goes against good coding practices. When you write a routine it should focus on changing one thing, and returning a value. Routines that side effect multiple arguments are harder to debug and more difficult to follow when troubleshooting. Routines that operate like this are usually indicative of a poor design. Feb 26 '07 #6

 100+ P: 1,806 I'm not sure that's necessarily true, As an example.... If you want to read from storage, there is little point being passed a buffer of some arbitrary length, when you don't know how long the data will be. If the methos is creating a buffer, it should also return the length of said buffer so that the caller knows how much data they're dealing with. Some people would suggest using another method and calling that "size" to acheive this, so that you first ask how big the data will be, then allocate a buffer that size, and pass it on to the read function. This idea has merit because the person allocating the buffer will be the person freeing it, however, by returning buffer and length together, we guarantee not (necessarily) that we return the length of data that should have been read, but the length of the ACTUAL buffer we created....that is, the caller does not need to make assume that we used their buffer as they intended. Of course alternatives such as creating a structure that stores the size and a pointer to the buffer may be a different solution....... Feb 26 '07 #7

 Expert 5K+ P: 5,000 ...Of course alternatives such as creating a structure that stores the size and a pointer to the buffer may be a different solution....... Yes this is an important point. It highlights the difference between programming in your language of choice and programming into you language of choice. Feb 26 '07 #8