double asterisk

Started by
5 comments, last by Lalle 22 years, 6 months ago
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
Advertisement
It means that ''a'' is a pointer to a pointer to an integer.
The fun of pointers increases dramatically as the number of asterisks increases! Hurray for dereferencing!
Consider the following function declaration:

void MyMoreConfusingFunc(int ********a);
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
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
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

This topic is closed to new replies.

Advertisement