Getting the Segmentation Error whenever I am trying to perform the string copy. but the same code will work when I will do direct assignment. In the if statement, I am performing the strcpy operation, getting the memory corruption issue. Help me to understand "strcpy" functionality with respective this code.
int getEnvVariable()
{
char mPath[20];
char mHome[20];
char *AddressHolder[2];
char *VariableName[2];
VariableName[0]="PATH";
VariableName[1]="HOME";
memset(&mPath,sizeof(mPath),0);
memset(&mHome,sizeof(mHome),0);
AddressHolder[0]=reinterpret_cast<char*>(&mPath);
AddressHolder[1]=reinterpret_cast<char*>(&mHome);
char** namePtr = VariableName;
char** vrbPtr = AddressHolder;
char* tmp;
while(*namePtr != NULL)
{
tmp = getenv(*namePtr);
cout<<"\n tmp - "<<tmp<<"\n";
if(tmp)
{
strcpy(*vrbPtr,tmp);
//*vrbPtr=tmp;
cout<<"\n"<<*namePtr<<": "<<*vrbPtr;
++namePtr;
++vrbPtr;
}
else
{
cout<<"\n Error: Environment veribale name are not set\n";
return -1;
}
}
cout<<"\n"<<VariableName[0]<<" : "<<mPath;
cout<<"\n"<<VariableName[1]<<" : "<<mHome;
return 0;
}