无法将字符串赋值给struct中的指针

时间:2013-06-15 18:15:42

标签: c arrays pointers struct

这是我的一段代码,我试图让它变得更简单我试图将一个字符串分配给一个数组内部的结构内的指针,我也想将指针初始化为NULL以便我可以检查是否或者没有医生使用房间...... 我不断收到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;
}

1 个答案:

答案 0 :(得分:2)

如果您想改为指定字符串用户strcpy

更改行

*clinic[i].doctorname = "fernando";

strcpy(clinic[i].doctorname, "fernando");