Reputation: 15091
For example why would one need char **myVariable;
? If a pointer is just an address in memory why what difference does it make if it's a pointer to an address with a pointer to an address of a char than just a pointer to an address with a char?
In assembly wouldn't this be like
LDR R3, =myVariable
LDR R2, =[R3]
LDR R1, =[R2]
LDR R0, =[R1]
Where a single pointer would be
LDR R1, =myVariable
LDR R0, =[R1]
now R0
holds the value? Obviously this way is faster.
Upvotes: 3
Views: 449
Reputation: 1066
In case you want to modify the value of pointer by a function, you will have to pass the address of that pointer to function by reference. Therefore you will need pointer to a pointer as argument.
Example :
int main()
{
int num=0;
int *p = #
// This function passes the pointer by value.
//So when function returns, *p points to same place
fn(p);
// This function will actually change where the pointer points to as
// it was passed by reference
fn2(&p);
}
void fn(int *ptr)
{
static int i=1;
ptr = &i;
}
void fn2(int **ptr)
{
static int j=1;
*ptr = &j;
}
Upvotes: 11
Reputation: 8587
First, because it's semantically possible and necessary to complete the language, leaving no undefined territory as to what does this expression mean, &p
where p
is a pointer?
And it's also quite handy as it seems. Here's an answer from Linus Torvalds. Using pointers to remove item from singly-linked list
Upvotes: 0
Reputation: 71566
int main ( int argc, char **argv )
an array of strings.
of course it is slower, you have the extra step in the middle, but without that extra step you cant have an array/table/list of pointers. You just have one.
Upvotes: 3
Reputation: 761
Replies above have address the first part of your question. Answer to second part "what difference does it make if it's a pointer to an address with a pointer to an address of a char than just a pointer to an address with a char?"
Though in terms of memory layout, what you are saying is right, C/C++ handles the pointer depending on its type. When you do pointer++, it increments the pointer by size of the data-type it is pointing to. if it is a pointer to an integer it will be incremented by 4, if it is a pointer to character it will be incremented by 1, if it is a pointer to a structure of size 20, it will be incremented by 20.
Upvotes: 2
Reputation: 10729
There are many more.
Upvotes: 0
Reputation: 70048
Pointer to a pointer is required when you want deal with storing pointer and dealing with it; e.g. to change the content of the underlying pointer. For example:
int i = 0, j = 1;
int* p = &i;
int** pp = &p;
Now you want to make p
point to j
instead of i
, then you can do as:
*pp = &j; // equivalent to `p = &j;`
This is just for explanation. In real world this is required when you are dealing with functions.
void Destroy (int** pp) { // for any int pointer
delete[] pp; // deallocate the memory
*pp = 0; // set the pointer to 0, so that it's not dangling
}
And use it as:
int* pi = new int[30];
...
Destroy(&pi); // `pi` is now pointing to 0
But still in C++, you have superior alternative as "pointer reference". Which does mostly the same thing, but with better readability.
void Destroy (int*& p) { // for any `int` pointer
delete[] p; // destroy the memory
p = 0; // Null out the same pointer which was actually passed
}
use as:
Destroy(pi); // no need to pass address
Upvotes: 1
Reputation: 26134
Well, if you have a table of pointers, a pointer to that table would have the type "pointer to pointer".
Upvotes: 10