这是我的一段代码,我试着让它更简单,我试着给数组中的一个结构内的指针分配一个字符串,同时我想把指针初始化为空,这样我就可以检查是否已经有医生在使用这个房间了。。。
我一直有seg错误
我很感激你的帮助
struct appointment{
char *SSN;
int status;//is appointment already taken?
};
struct room{
int doctorID;
char *doctorname;
struct appointment hours[10];
};
struct room clinic[5];
for(int i = 0; i < 5; i++){ //I was trying to initialize all pointers to NULL but didn't work
clinic[i].doctorID = 0;
for(int j = 0; i < 10; i++){
clinic[i].hours[j].status = 0;
}
}
for(int i = 0; i < 5; i++){
clinic[i].doctorname = malloc(sizeof(char) * 30); // Am I doing something wrong here?
*clinic[i].doctorname = "fernando";
printf("the name of the doctor on clinic %d is %s\n", i, clinic[i].doctorname
free(consultorios[i].medico);
}
return 0;
}
最佳答案
如果要改为指定字符串userstrcpy
。
改变你的路线
*clinic[i].doctorname = "fernando";
到
strcpy(clinic[i].doctorname, "fernando");