FroxX wrote:
Hi all,
i have a question.
What does those ** mean???
For example:
void function(Something **pSomething);
I hope someone can help me about it.
Thanks,
FroxX
A common example where this is used is the main function
#include <iostream>
int main(int argc, char** argv){
//[...]
}
In this case char** argv can be seen as an object containing many strings.
In C every array can by described as a pointer:
char array[] = "hello world";
is the same as
char* p_array = array;
The name of the array is the same as a pointer to the first element of the array.
As you probably know, a string is an array of chars in C.
If you want to pass many strings (which means many arrays of chars) as an
argument to a function for example (as it also happens with the main function),
you can store the pointers to each string in another array.
You will then have an array of pointers. (Again: Each of these pointers point to
an array, representing a string )
This array can be seen like this;
char* my-strings[] = { "hello world", "bye world" };
This pointer is the same as the array above
char** p_my-strings = my-strings;
because char** p_my-string is a pointer to the first element of char*
my-string[], which is a represenation of an array, as I wrote above. In this
case it represents an array of pointers to arrays.
Though this seems a bit confusing, it isn't that hard to understand.
Don't imagine an array as many elements in a row, but as an object. If you use
pointers to the first element of an array as representation of the array, you in
fact have one object only - the pointer. The only case, you should imagine an
array as a list of elements, is when you access the elements in the array.
Again, if you have an array of arrays, you have a pointer, to many pointers,
which is why it's written as char**
The version char** is needed, if you dynamically allocate memory with malloc(),
since malloc always returns pointers to the memory it has reserved. The returned
pointer is the only way to access the reserved memory. This programming
technique is important if you want to programm in a secure and resource friendly
way.
pointers to pointers are not limited to chars. This can be done with any kind of
datatype, including structs.
a pointer to a pointer is not necessarily an array of arrays, but this is what
it's used for in 90% of all cases. I actually don't know a case where else you
need pointers to pointers.
If you are programming a table (image you were the programmer of EXCEL). Then
each field of the table might be a pointer to a struct, which represents the Field.
The table then consists of pointers to pointers to pointers, why you'd say
Field*** table;
greets Boris