Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用结构和指针数组时出错:赋值中的类型不兼容_C_Pointers_Arrays_Struct - Fatal编程技术网

C 使用结构和指针数组时出错:赋值中的类型不兼容

C 使用结构和指针数组时出错:赋值中的类型不兼容,c,pointers,arrays,struct,C,Pointers,Arrays,Struct,此代码在personen[0]->sName=“Pieter”上生成错误在分配中说出不兼容的类型。为什么?不要尝试分配数组。使用strcpy将字符串从一个数组复制到另一个数组 …sName是一个字符数组,而“Pieter”是一个常量字符*。不能将后者指定给前者。编译器总是正确的:)不要尝试分配数组。使用strcpy将字符串从一个数组复制到另一个数组 …sName是一个字符数组,而“Pieter”是一个常量字符*。不能将后者指定给前者。编译器总是正确的:)更改 #define STRMAX 50

此代码在
personen[0]->sName=“Pieter”上生成错误在分配中说出不兼容的类型。为什么?

不要尝试分配数组。使用
strcpy
将字符串从一个数组复制到另一个数组


sName
是一个字符数组,而“Pieter”是一个
常量字符*
。不能将后者指定给前者。编译器总是正确的:)

不要尝试分配数组。使用
strcpy
将字符串从一个数组复制到另一个数组

sName
是一个字符数组,而“Pieter”是一个
常量字符*
。不能将后者指定给前者。编译器总是正确的:)

更改

#define STRMAX 50

struct Person {
    char sName[STRMAX];
    int iAge;
};
typedef struct Person PERSON;

int main() {
    PERSON *personen[1];
    personen[0]->sName = "Pieter";
    personen[0]->iAge = 18;

    return 0;
}

并使用strcpy复制字符串

PERSON personen[1];
改变

并使用strcpy复制字符串

PERSON personen[1];

您不需要指针数组。试试
PERSON-personen[1]


正如其他人所说,使用strcpy函数

您不需要指针数组。试试
PERSON-personen[1]


正如其他人所说,使用strcpy函数

我同意上面的说法,但我认为包括“为什么”也很重要

要从简单指针中获取b和c,并为它们提供匹配d的内存,请使用以下命令为它们提供内存空间

int a;      // is an integer
int *b;     // pointer to an integer must be malloced (to have an array)
int c[];    // pointer to an integer must also be malloced (to have an array)
int d[5];   // pointer to an integer bu now it is initialized to an array of integers

它将从malloc返回的指针强制转换为int指针,并创建一个5倍于整数大小的内存块(因此它将保存5个像d一样的整数)

我同意上述观点,但我认为包含“为什么”也很重要

要从简单指针中获取b和c,并为它们提供匹配d的内存,请使用以下命令为它们提供内存空间

int a;      // is an integer
int *b;     // pointer to an integer must be malloced (to have an array)
int c[];    // pointer to an integer must also be malloced (to have an array)
int d[5];   // pointer to an integer bu now it is initialized to an array of integers
其中,它将从malloc返回的指针强制转换为int指针,并创建一个5倍于整数大小的内存块(因此它将保存5个类似于d的整数)

b = (int *) malloc(sizeof(int)*5);