lets look at a simple little example function:
CODE
void setVal(int *ptr) {
int value = 100;
ptr = &value;
}
int main() {
int val = 0;
setVal(&val);
cout << "Value: " << val;
return 0;
}
this would print out "Value: 0" rather than 100, why?
The reason is that the argument (in this case &val) is stored on the stack, and the variable ptr is assigned to that location on the stack, when I change that value I am in no way affecting the value stored in the address &val. This is exactly what is happening with your string. You pass a pointer to some string, you then change that pointer, but NOT what the pointer points to. What is funny to me is that you did things right with the int in the same function!
So how to get your function to work?
Well first off you need to create a string to pass a reference to (or pass a pointer to a pointer). Then you need to change what the pointer points to, not the pointer:
CODE
#include <iostream>
#include <cstring>
using namespace std;
void initialize(char [], int *);
int main() {
char name[100]; //setup a string that will need to be intialized
int age;
initialize(name, &age);
cout << "Name: " << name << "\tAge: " << age << endl;
return 0;
}
void initialize(char name[], int *age) {
strcpy(name, "Hello"); //this will copy the string into the location
*age = 100;
}
The other way to do this would be to use a pointer to a pointer:
CODE
#include <iostream>
#include <cstring>
using namespace std;
void initialize(char **name, int *age);
int main() {
char *name;
int age;
initialize(&name, &age);
cout << "Name: " << name << "\tAge: " << age << endl;
delete[] name;
return 0;
}
void initialize(char **name, int *age) {
*name = new char[100]; //create the buffer
strcpy(*name, "Hello"); //copy in the new value
*age = 100;
}