这是我的一段代码,我试图让它变得更简单,我试图将一个字符串分配给数组内结构内的指针,我还想将指针初始化为 NULL,以便我可以检查是否或者不是已经有医生在使用这个房间了...... 我不断收到段错误错误 我将不胜感激任何帮助
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;
}
最佳答案
如果您想分配一个字符串用户strcpy
。
更改线路
*clinic[i].doctorname = "fernando";
至
strcpy(clinic[i].doctorname, "fernando");
关于无法将字符串分配给结构内的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17126546/