i have a problem regarding the output of char ptr. following is the sample code which i am trying to execute..
struct DeviceInfo{
char * name;
int id;
};
void testFunc(DeviceInfo *info){
char temp[50] = "test input";
info->name = temp;
}
void main(){
DeviceInfo deviceInfo;
testFunc(&deviceInfo);
std::cout<<"Output is "<<deviceInfo.name;
}
Output i get in the main is some kind of weird one .... while debugging i find out that when "test.name" is passed on to output stream it not only gives garbage output but it also changes the "name" value for "deviceInfo" object to that garbage value... this is kind of test scenario but in actual application this testFunc has to be executed in the same way i.e. constant char string should be assigned name to char ptr.
i tried puting '\0' at the end of char temp like this temp[strlen(temp)] = '\0';
but it still dont work
Any help is appreciated...
Raza