Archived

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

double asterisk

This topic is 5897 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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