Possible Duplicate:
How do pointer to pointers work in C?
Hi All,
I have very basic question : What is the use of double pointers in C or C++ ?
Can somebody explain me that with some examples, please.
Thanks,
Sen
Possible Duplicate:
How do pointer to pointers work in C?
Hi All,
I have very basic question : What is the use of double pointers in C or C++ ?
Can somebody explain me that with some examples, please.
Thanks,
Sen
If you want to change what the original pointer points to in a function call you need a **. This is because when you pass a pointer you are passing a copy of a pointer, i.e., if you modify what the pointer points to inside of a function (i.e., changing the value of the pointer itself) it is reflected in the copy but not the original (different than modifying what the original pointer points to already, instead pointing it to an entirely new object).
Also, multi-dimensional arrays can be accessed by pointer to pointer (to pointer to pointer... ad infinitum).
A regular data pointer is an address of some variable.
Pointers themselves are also variables and therefore have addresses, which would therefore be pointers to pointers.
Pointers are used more in C than in C++ because in C++ there is an alternative syntax using references, as well as the fact that many times pointers are wrapped in classes and not used quite as directly.
In C, the following example you free the structure in the pointer and it also sets the pointer passed in to NULL.
void FreeMyStruct( struct MyStruct ** ptr )
{
free((*ptr)->resource);
free(*ptr);
*ptr = NULL;
}
You could do the same with a pointer but if you set it to NULL it would have no effect on the caller of the function because it would be a copy of their pointer you would be setting to NULL.
En example from C standard:
long int strtol(
const char * restrict nptr,
char ** restrict endptr,
int base);
...A pointer to the final string is stored in the object pointed to by endptr, provided that endptr is not a null pointer.
If the subject sequence is empty or does not have the expected form, no conversion is performed; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a null pointer.
Function body manipulates with copies of its arguments. So when you want to change some variable that you pass as function argument, you actually need to pass variable's address - its pointer. Function gets a copy of that pointer which has an address of original variable so function can change variable's value. In the same way, when you want to change some pointer's value, you need to pass its address as a function argument.
Typical example would be a case when function needs to reallocate memory for some data structure. In this example, we want to pass a character array into a function which appends a single character to it. Function needs to reallocate memory for a new (extended) array which means that its pointer needs point to a new location in memory - it value needs to be changed. Pay attention to lines where *pp (dereferenced pointer to pointer) is assigned a value:
#include <iostream>
using namespace std;
void append_char(char** pp, char ch)
{
// copy of pp is passed here
if(pp)
{
if(*pp)
{
// pointer is already pointing to formed array of characters
// 1. allocate memory for a new (extended) array
int nLen = strlen(*pp);
// one place for ch and one for zero-termination
int nNewLen = nLen + 2;
char* pNewStr = new char[nNewLen];
// 2. copy existing array into a new one
strcpy(pNewStr, (const char*)*pp);
// 3. append new character
pNewStr[nNewLen - 2] = ch;
pNewStr[nNewLen - 1] = '\0';
// 4. free memory of old array
delete[] *pp;
// 5. assign new pointer's value
*pp = pNewStr;
}
else
{
// pointer is NULL - passed character will be the first in a new array
// 1. allocate memory, fill the array and assign new pointer's value
*pp = new char[2];
(*pp)[0] = ch;
(*pp)[1] = '\0';
}
}
}
int main()
{
char* p = new char[3];
p[0] = 'A';
p[1] = 'B';
p[2] = '\0';
cout << p << endl;
append_char(&p, 'C');
cout << p << endl;
delete[] p;
return 0;
}
I used C-style string here as it's common example of memory management. In your C++ code try to avoid C strings and use C++ cast operators.
one thing that comes to my mind right away -
When you use "pass by pointer" to pass a pointer to a function, only a copy of the pointer is passed to the function. We can say "pass by pointer", it is actually passing a pointer by value. In most cases, this does not present a problem. But, a problem arises when you modify the pointer inside the function. Instead of modifying the variable, you are only modifying a copy of the pointer and the original pointer remains unmodified, that is, it still points to the old variable