Archived

This topic is now archived and is closed to further replies.

Lalle

double asterisk

Recommended Posts

What does it mean when a function has the following decleration? void MyFunc(int **a); What does it mean when you have two asterisks like this? Thanks

Share this post


Link to post
Share on other sites
The fun of pointers increases dramatically as the number of asterisks increases! Hurray for dereferencing!

Share this post


Link to post
Share on other sites
They are also used to modify the address of a pointer, ie
void f()
{
char *Thingy = "Hello\nThere";

// Thingy = "Hello...
NextCRString( &Thingy );
// Thingy = "There"
}

void NextCRString( char **a )
{
while( (*a) != ''\n'') ) a++;
(*a)++;
}

Could also use

void NextCRString( char *&a ); so you dont have to pass the address of Thingy and derefence a.


[The views stated herein do not necessarily represent the view of the company Eurocom ]

Eurocom Entertainment Software
www.Eurocom.co.uk

The Jackal

Share this post


Link to post
Share on other sites
If you want a function to reallocate a pointer the pointer will either have to be passed by reference (which isn''t possible in C), or as a pointer to a pointer.

Consider the following:
  
void myFunctionThatDoesntWork (MyStruct* s) {
free (s);
s = (MyStruct*)malloc(sizeof(MyStruct)); // WRONG, only

// local copy of

// s is reallocated

}

void myFunctionThatWorks (MyStruct** s) {
free (*s);
*s = (MyStruct*)malloc(sizeof(MyStruct)); // CORRECT

}

void myCPlusPlusFunctionThatWorks (MyStruct*& s) {
delete s;
s = new MyStruct(); // CORRECT

}


-Neophyte

Share this post


Link to post
Share on other sites