Other alternatives...
#2 - Pass a pointer-to-a-pointer:
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
struct person_details{
char name[100];
char phone[100];
};
void getDetails( person_details ** pd ){
*pd = new person_details;
strcpy( (*pd)->name, "John Doe" );
}
int main(int argc, char *argv[])
{
person_details *pDetails = 0;
getDetails( &pDetails );
cout << "pDetails->name " << pDetails->name << endl;
delete pDetails;
system("PAUSE");
return EXIT_SUCCESS;
}
#3 - Return a pointer from the getDetails function:
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
struct person_details{
char name[100];
char phone[100];
};
person_details * getDetails()
{
person_details * pd = new person_details;
strcpy( pd->name, "John Doe" );
return pd;
}
int main(int argc, char *argv[])
{
person_details *pDetails = getDetails();
cout << "pDetails->name " << pDetails->name << endl;
delete pDetails;
system("PAUSE");
return EXIT_SUCCESS;
}