Click to See Complete Forum and Search --> : Pointers


caffineplease
June 26th, 2002, 08:16 AM
Hi Guru's,
I don't know if I've missed something here but I've looked into the subject of pointers to pointers (i.e. char** varName) and I can't see what the point is to them and how they can be used in normal everyday development.


Can anyone shed any light as to why we need pointers to pointers and what can they be used for?

Regards

John

jfaust
June 26th, 2002, 08:25 AM
They are usually used as arguments to methods that allocate memory for the pointer. See, if you pass in a single pointer, the function can change what the pointer points to, but cannot change the pointer address itself. To do this, you need a pointer to a pointer.

Jeff

caffineplease
June 26th, 2002, 08:38 AM
So how can I use them??

I'm trying pass a char** to a function as char*&. When going into debug mode, the string is coming up as Error:Expression cannot be evaluated.

What I'm tring to do is change the contents of that string in Func1.


main()
{
char** cppString;

//Some code

func1(cppString);
}

void func1(char*& cprString)
{
// want to add chars to string but giving me violation error;

}

Can u tell me what I'm doing wrong please?

regards

Federal102
June 26th, 2002, 08:51 AM
Looks like you're trying to pass a reference to a function...

One way to do it would be to use std::string instead of char arrays.

void func(string &myString)
{
myString += "a";
};

int main()
{
string myString;
func(myString);
cout << myString << endl; // prints a
return 0;
}

jfaust
June 26th, 2002, 09:20 AM
Ugh, I hate pointers to references, or references to pointers, or whatever they are. I avoid these at all costs. It would be better to use a pointer to a pointer.

To answer your question, pass it in as '*cppString'.

Jeff

Federal102
June 26th, 2002, 09:30 AM
One of my favorite quotes from "C++ FAQs".......

Q "Why use references when pointers can do everything that references can do?"

A "Using a pointer when a reference will do is like using a chain saw to trim your fingernails - it will do the job, but you'd better be careful.

caffineplease
June 26th, 2002, 09:31 AM
complete Nightmare if u ask me!!!!!!!!!

jfaust
June 26th, 2002, 09:45 AM
Well, wether to use pointers or references is a topic in itself, and a source of some debate. However, using *& is overly confusing, and should be avoided simply for maintanance reasons. The closest thing to this is a pointer to a pointer.

In this example, a std::string reference would probably work the best, as Federal102 indicated.

Jeff

Federal102
June 26th, 2002, 09:50 AM
I agree. I have never seen a pointer to a reference. Very confusing!!

Alexis Moshinsky
June 26th, 2002, 10:33 AM
Hi All,

*& is sometimes used instead of **. The most popular example is
restoring of objects.

friend CArchive& operator >>( CArchive& ar, CObject *& pOb );
throw( CArchiveException, CFileException, CMemoryException );

JMS
June 26th, 2002, 04:17 PM
>> So how can I use them??

>>I'm trying pass a char** to a function as char*&. When going
>> into debug mode, the string is coming up as Error:Expression
>> cannot be evaluated.

>> What I'm tring to do is change the contents of that string in
>> Func1.


In olden days C did not have the concept of passing by refference. Only passing by value. So any parameter ( in olden days ) passed into a function could not be changed i that function and remain changed with the function ended.

So....
main()
{
char *pszBuff = NULL; // initialize pointer

MyFunc ( pszBuff ); // Call a function to change pointer

printf ( "%s", pszBuff ); // print the buffer
return;
}

int MyFunc ( char *pszString )
{
pszString = new char [20];
strcpy( pszString, "Hello World\n");
return(0);
}


Would print out NULL since the value of the pointer was changed inside the function and the parameter was a value not a refference to the original variable...


So old clever and pocket guarded C programmers got around this lack of an ability to pass by refference with passing the pointer to the ponter and this passing enabling functions to change paramters like this...

main()
{
char *pszBuff = NULL; // initialize pointer

MyFunc ( & pszBuff ); // Call a function to change pointer

printf ( "%s", pszBuff ); // print the buffer
return;
}

int MyFunc ( char **ppszString )
{
*pszString = new char [20];
strcpy( pszString, "Hello World\n");
return (0);
}

Now your printf command actually prints out Hello World....

Ok enough of ancient history. In modern times we have fire, electricity, running water and refferences..... The reason one would use a refference to a pointer would be Identical to the reason one would use a pointer to a pointer. So you could allocate memory inside the function and not loose that memory when the function returned.



What you are doing Wrong!!....

// here is your code
main()
{
char **cppString; // <---- pointer 2 pointer..

//Some code

func1(cppString);
}

void func1(char*& cprString) // <--- refference to pointer
{
// want to add chars to string but giving me violation error;

}


Refference to pointer is not the same as pointer to pointer!!

you could do this to fix your code..

main()
{
char* cppString; // <---- pointer

//Some code

func1( &cppString); // <----- pointer to pointer '&' takes address of variable
}

void func1(char **cprString) // <--- pointer to pointer
{
// want to add chars to string but giving me violation error;
*cprString = new char[100];
strcpy( cprString, "Hello World");
}


Get it?

some things to read up on...

The & operator which when used in front of a variable takes the address of that variable. Instandly turns an object or variable into a pointer of that object or variable.

The * operator which when used in front of a pointer to a variable turn the result into the actual variable. So it says go to that address..

int i = 1;

*(&i) = 1

Get it?

JMS
June 26th, 2002, 04:28 PM
Now if your not totally screwed up let me throw something else at you... Strings look like varables.. but they aren't. Strings are actually pointers. So if alls you wanted to do was change a string as you say and not change the pointer value of that string you could do this and it works fine..


main()
{
char szBuffer[100] = "";

ChangeString( szBuffer );
}

void ChangString( char *pszBuffer)
{
strcpy( pszBuffer, "Hello World" );
return;
}

Now szBuffer is a pointer remember a pointer to 100 bytes of memory. All arrays are pointers. If you don't believe me check this out..

pszBuffer[2] == *(pszBuffer+2)

See what I'm saying?

what I did in my function was change the value stored in those 100 bytes of data to "Hello World". So this change will work since the memory wasn't passed in only the pointer to that memory.

Now this should not work

main()
{
char szBuffer[100] = "";

ChangeString( szBuffer );
}

void ChangString( char szBuffer[100])
{
strcpy( pszBuffer, "Hello World" );
return;
}

Of course with optimizing compilers now days, the compiler is smart enough to change your code and keep you from loading the entire 100 byte array onto the stack. Thus even though it shouldn't work, it does work....

caffineplease
June 27th, 2002, 05:57 AM
Thanks to everyone who answered my query. Its a pity that this site no longer allows a points scoring recomendation.

So how I understand it is that ** notation can help preserve both the contents and the size of the memory chunck when going in and out of a function. But since I am doing this in a DLL I am getting ESP dialog msgs when I'm comming out of my DLL back into my calling program code. Does anyone have any ideas as to what maybe the problem?

Regards