Это потому, что struct Address * add [2] - это массив указателей на структуру типа Address. Эти указатели недействительны, потому что вы их не выделили. Это должно быть что-то вроде этого
node->add[0] = malloc(sizeof(struct Address))
node->add[1] = malloc(sizeof(struct Address))
Это также не удастся, потому что вы пытаетесь скопировать строку, но строка также не выделяется.
node->add[0]->streetName = (char*)malloc(stringSize)
node->add[0]->city= (char*)malloc(stringSize)
После этого назначение должно работа.
Редактировать:
struct Address
{
char* streetName;
char* city;
};
struct Employee {
char name[32];
int empId;
struct Address* add[2];
struct Employee* next;
};
struct Employee* createEmployee(int id, const char *name, const char *street1, const char *city1, const char *street2, const char*city2)
{
struct Employee* emp = (struct Employee*)malloc(sizeof(struct Employee));
emp->empId = id;
strcpy(emp->name, name);
emp->add[0] = (struct Address*)malloc(sizeof(struct Address));
emp->add[0]->streetName = (char*)malloc(256);
emp->add[0]->city = (char*)malloc(256);
strcpy(emp->add[0]->city, city1);
strcpy(emp->add[0]->streetName, street1);
emp->add[1] = (struct Address*)malloc(sizeof(struct Address));
emp->add[1]->streetName = (char*)malloc(256);
emp->add[1]->city = (char*)malloc(256);
strcpy(emp->add[1]->city, city1);
strcpy(emp->add[1]->streetName, street1);
return emp;
}
void insertEmployee(struct Employee** head, struct Employee* emp)
{
if (*head == NULL)
{
emp->next = NULL;
*head = emp;
}
else
{
emp->next = (*head);
*head = emp;
}
}
void printList(struct Employee* head)
{
while (head != NULL)
{
printf("%s\n", head->name);
printf("%d\n", head->empId);
printf("%s\n", head->add[0]->city);
printf("%s\n", head->add[0]->streetName);
printf("%s\n", head->add[1]->city);
printf("%s\n", head->add[1]->streetName);
head = head->next;
}
}
int main()
{
struct Employee* head = NULL;
struct Employee* emp = NULL;
emp = createEmployee(1, "name1", "street1", "city1", "street2", "city2");
insertEmployee(&head, emp);
emp = createEmployee(2, "name2", "street3", "city3", "street4", "city4");
insertEmployee(&head, emp);
printList(head);
return 0;
}