无法将字符串分配给结构内的指针

标签 c arrays pointers struct

这是我的一段代码,我试图让它变得更简单,我试图将一个字符串分配给数组内结构内的指针,我还想将指针初始化为 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/

相关文章:

C数组衰减消歧

c - 变量返回还是直接返回?

c - 错误 : (195) expression syntax

java - 根据奇怪的协议(protocol)将整数放入字节数组

arrays - 我们如何在Rust中操纵u8数组的第一位? [复制]

c - 避免私有(private)函数的原型(prototype)声明(在使用前定义)是否违反 MISRA?

php - 将 PDO 结果集数组转换为可访问的数组 PHP

c++ - const int*、const int * const 和 int const * 之间有什么区别?

c++ - C 中的 * 和 & 有什么区别?

c - 用整数和字符刷新指针