#include <stdio.h>
#include <string.h>
typedef struct
{
char firstName[50];
char lastName[50];
int age;
}Person;
void ModifyPerson(Person*);
int main()
{
Person person;
strcpy(person.firstName, "firstName");
strcpy(person.lastName, "lastName");
ModifyPerson(&person);
printf("First Name=%s\nLast Name=%s\n",person.firstName, person.lastName);
system("PAUSE");
}
void ModifyPerson(Person *p1)
{
//This doesn't modify the original variable declared in main function
Person person = *p1;
strcpy(person.firstName, "Modified1");
strcpy(person.lastName, "Modified1");
//This does modify the original person variable
strcpy((*p1).firstName, "Modified");
strcpy((*p1).lastName, "Modified");
}
In the above code, the first 3 lines in method ModifyPerson fails to modify the original "person" variable declared in main method. The changes made by the first 3 line of code in the ModifyPerson method is local to the variable. However the changes it makes using the last 2 line affects the original "person" variable created in main method.
Now what I'm trying to find out is if there is a way to work with *p1 within the ModifyPerson method. As you see, it's not always convenient to write (*p1) each time i want to use this variables value. Is there any other way round for this ?
Please help ! I'm new to C. I have been working with C# so far.