What you are doing wrong...
>> 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?