fillMe is a copy of the pointer, so you're allocating memory to a copy, then the copy is destroyed, thus destroying your only reference to the memory. This is affectionately called a memory leak. The solution in C is to pass a pointer to the pointer so that you can get to the original object:
#include <cstring>
#include <iostream>
using namespace std;
void test( char ** fillMe )
{
*fillMe = new char[10];
strcpy( *fillMe, "TESTING" );
}
int main()
{
char * tester = NULL;
test( &tester );
if ( tester == NULL )
{
cout<< "tester is null"<<endl;
}
else
cout<< tester <<endl;
}
Notice that I included <cstring>, which is required for memset and strcpy, and I also removed memset because there's no point in using it since you'll just use strcpy right after.
The C++ version using references is even easier:
#include <cstring>
#include <iostream>
using namespace std;
void test( char *& fillMe )
{
fillMe = new char[10];
strcpy( fillMe, "TESTING" );
}
int main()
{
char * tester = NULL;
test( tester );
if ( tester == NULL )
{
cout<< "tester is null"<<endl;
}
else
cout<< tester <<endl;
}
If I might direct you
here, you'll probably have an easier time with pointers.
I'm here to prove you wrong.