Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What is **

Reply
Thread Tools

What is **

 
 
FroxX
Guest
Posts: n/a
 
      09-04-2004
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
 
Reply With Quote
 
 
 
 
Moonlit
Guest
Posts: n/a
 
      09-04-2004
Hi,

"FroxX" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi all,
> i have a question.
> What does those ** mean???
> For example:
>
> void function(Something **pSomething);


It is a pointer to , a pointer to something.

This is usually done when you want to assign something to a pointer for
instance

#include <stdlib.h>
#include <memory.h>


void MyAlloc( void **ptr )
{
*ptr = malloc( 1000 );
}


int main()
{
void *Ptr=0;
MyAlloc( &Ptr );

// do something not so useful
memset( Ptr, 0, 1000 );

free( Ptr);

return 0;
}


>
> I hope someone can help me about it.
>
> Thanks,
> FroxX



 
Reply With Quote
 
 
 
 
Boris Glawe
Guest
Posts: n/a
 
      09-04-2004
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



 
Reply With Quote
 
FroxX
Guest
Posts: n/a
 
      09-04-2004
hi Boris,

wie ich sehe bist du aus deutschland.
Danke für die ausführliche Erklärung!!!

//In English:
I see you're from Germany.
Thanks for the description!!!

CU, FroxX
FroxX, Germany, NRW, C++ Beginner, Froxxinating.de (excuse it, but i didnt work
on it for long) )
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments