Pretty much what it sounds like. I know that using pointers is obviously quite useful in C++, but Ive never understood what the use of pointers to pointers could be. Wouldn't it just waste memory defining the adresses at the pointer to pointer level? Is there a hidden use for them, or was it just included for the sake of flexibility in C++?
Usually a pointer to a dynamically allocated array ends up being a pointer to a pointer, or a bunch of pointers in that case. MS's COM does this A LOT which is probably the most difficult part about it to wrap your head around. I've also caught myself doing it while I was playing around with Polymorphism too, you start out with an array of pointers to base objects, you find yourself wanting to pass it to a function and before you know it you end up dereferencing objects in order to dereference them.
You would see this a lot more in C code than in C++.
Consider if you had to write a stack data structure in C, you might have something like this:
1 2 3 4 5 6 7 8 9 10 11
typedefstruct node
{
int value;
node* next;
} Node;
int main( void )
{
Node *my_stack;
// ...
}
If you wanted to write a function that added to the stack, you need that function to do two things. The first is of course to create the new node and put it on the top of the stack. The second is that the my_stack pointer would have to now point to the new node. This means that you would not be able to pass my_stack by value, you would need to pass a pointer to my_stack ( which is a pointer )
You'd use a pointer to a pointer for the same reasons you'd use a pointer to any other data type. For example, you might want to pass a pointer into into a function that changes the value of that pointer, and you might have a reason to prefer using a pointer to the pointer, rather than a reference to it.
Or you might want to have an array of pointers, just like you might want an array of ints. So you can manipulate the array using a pointer to a pointer, just like you'd manipulate an int array using a pointer to int.
A pointer is just a variable, like any other. You want the same ability to manipulate that variable type that you would any other type.
Which is to say - as long as you're assigning a valid memory address that you know contains valid data, it's fine. But that's true of the initial assignment, too.
But... Wouldnt editing the value of a pointer (ie the address in memory that the pointer is listed at) be rather dangerous?
Why would you think so?
1 2 3 4 5 6 7 8 9
int One = 1;
int Two = 2;
int *Ptr = &One; //Initializing the pointer
std::cout << *Ptr << std::endl;
Ptr = &Two; //Changing the address the pointer holds
std::cout << *Ptr << std::endl;
You could try to hard code the address of the pointer but, just like jumping out an window and trying to use your neck as a pogo-stick, it's really not a good idea.
No, it wouldn't shift it by one byte, it would shift it by the size of one float (which would be more than 1 byte, but defined by the implementation). You might want to read up on pointer arithmetic.
It would be a silly thing to do in the case where your pointer points to a single float. On the other hand, it would be a perfectly normal thing to do if your pointer pointed to the first float in an array.
So... um... do sensible things and not stupid ones, I guess.