typedef struct unit_class_struct {
char *name;
char *last_name;
} person;
int setName(person *array) {
array[0].name = strdup("Bob");
array[1].name = strdup("Dick");
return 1;
}
int setLastName(person *array) {
array->last_name = strdup("Sanchez");
array++;
array->last_name = strdup("Clark");
return 1;
}
int main()
{
person array[10];
person *pointer;
pointer = array;
setName(pointer);
setLastName(pointer);
printf("First name is %s %s\n", array[0].name, array[0].last_name);
printf("Second name is %s %s\n", array[1].name, array[1].last_name);
while(1) {}
return 0;
}
This is some example code I came up with to play around with structures. Notice the way I set the name in setName and the way I did it in setLastName.
Both work, but I'm curious whats the difference between the two ways I did it?
Is one way better than the other?
Also is strdup necessary in this example? If not, would it be necessary if I was setting array.name to random sized variables rather than string literals?