如何构造结构数组,如何称呼它们?我想列出驱动程序及其公司的结构清单。现在,代码仅存储一个结构,但是在尝试存储另一个结构后出错。它在某人处停止= realloc(somebody,(sizeof(struct driver)*(j + 1)));由于某些原因。
struct Timer {
int hour;
int minutes;
int seconds;
};
struct driver {
char* name;
char* company;
int total;
struct Timer time;
};
char AddName(struct driver* somebody, int j) {
char name[30];
char company[30];
scanf("%s", name);
scanf("%s", company);
somebody = realloc(somebody, (sizeof(struct driver) * (j + 1)));
somebody[j].company = company;
somebody[j].name = name;
somebody[j].time.seconds = 0;
somebody[j].time.minutes = 0;
somebody[j].time.hour = 0;
somebody[j].total = 0;
return somebody;
}
int main(void) {
char text[100];
int num_drivers = 0;
struct driver* somebody = malloc(sizeof(struct driver));
printf("Enter text\n");
scanf("%s",text);
printf("-------------------------------------------------\n");
while (text != "Q") {
if (strcmp(text,"A") == 0) {
somebody = AddName(somebody,num_drivers);
num_drivers++;
}
A Sam Renault
A Frank Volvo