C中不兼容的指针类型

时间:2017-11-09 05:51:14

标签: c structure

我一直在讨论这个问题已有一段时间但我仍然得到同样的警告:

"incompatible pointer types passing 'char *[15]' to parameter of type 'char *' [-Wincompatible-pointer-types]:
strncpy(person->firstName, fn[j], NAME_SIZE - 1);"

有问题的源代码是:

while (i < numPersons) {
    j = rand() % NUM_NAMES;
    person->firstName[NAME_SIZE - 1] = "\0";
    strncpy(person->firstName, fn[j], NAME_SIZE - 1);
    j = rand() % NUM_NAMES;
    person->familyName[NAME_SIZE - 1] = "\0";
    strncpy(person->familyName, sn[j], sizeof(person->familyName) - 1);
    if (rand() % 2) {
        person->emplyeeOrStudent = 1;
        populateStudent(&person->stu);
    } else {
        populateEmployee(&person->emp);
        person->emplyeeOrStudent = 0;
    }
    person++;
    i++;
}

这是结构人:

struct Person{
    char *firstName[15];
    char *familyName[15];
    char *telephone[10];
    union combine comb;
    unsigned int emplyeeOrStudent:1;
};

1 个答案:

答案 0 :(得分:1)

当你传递person-&gt; firstName时,你基本上传递char **而不是char * 第二 - 如果没有预先分配人员,你的副本将失败,因为你无法复制到在数据段上分配的char *字段,基本上是常量!