I have problem allocating memory for array of structure containing char*.
I have one stracture "Person"
typedef struct
{
char *name;
char *surname;
char *phonenumber;
} Person;
What I want to do is to read some data from file and fill the array (Person *array) of people where I have to dynamically allocate memory.
At the moment, I have something like that:
array = malloc(sizeof(Person) * arraysize);
Person *buff;
char text[100];
char *result;
for(i=0; i<arraysize; i++)
{
buff = &array[i];
fgets(text, 100, f );
//Read first name
result = strtok(text,":");
buff->name= malloc(strlen(result));
buff->name= result;
//Read surname
result = strtok(0,":");
buff->surname = malloc(strlen(result));
buff->surname = result;
//Read phone number
result = strtok(0, ":");
buff->phonenumber = malloc(strlen(result));
buff->phonenumber = result;
}
When I print out the whole array I don't get any valid data. I'm wondering what am I doing wrong. I appreicate for your answers in advance!