double asterisk
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
The fun of pointers increases dramatically as the number of asterisks increases! Hurray for dereferencing!
Pointers to pointers are used for passing 2D arrays to functions, theres prolly other uses but I''ve never used em.
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
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
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:
-Neophyte
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement