Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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中,将struct char数组赋值给char数组会给出一个;“分配中的不兼容类型”;错误_C_Arrays_Struct_Char - Fatal编程技术网

在C中,将struct char数组赋值给char数组会给出一个;“分配中的不兼容类型”;错误

在C中,将struct char数组赋值给char数组会给出一个;“分配中的不兼容类型”;错误,c,arrays,struct,char,C,Arrays,Struct,Char,我试图将大小为88的字符数组分配给结构的大小为88的字符数组属性,但编译程序时,我得到: 错误:分配中存在不兼容的类型 这里是我定义结构的地方: #define BUFFERSIZE 9 /* Bounded Buffer item structure */ typedef struct item { int id; /* String index value */ char str[88]; /* String value */ }item; 我将该项声明为全局变

我试图将大小为88的字符数组分配给结构的大小为88的字符数组属性,但编译程序时,我得到:

错误:分配中存在不兼容的类型

这里是我定义结构的地方:

#define BUFFERSIZE 9
/* Bounded Buffer item structure */
typedef struct item {
   int  id;       /* String index value */
   char str[88];  /* String value */
}item;
我将该项声明为全局变量:

item buffer[BUFFERSIZE];
int bufferFill;
我正在另一个文件中设置属性,因此我也在另一个文件中使用extern声明它们:

extern item buffer[BUFFERSIZE];
extern int bufferFill;
在我将它们重新声明为外部全局变量的同一文件中的函数内部,我声明了一个新的char数组:

char outputString[88];
我最初的想法是:

char * outputString; 
这就产生了与我仍然得到的错误相同的错误,因此我尝试对其进行更改,以便使用与结构中相同的大小声明数组

在用数据填充OutputString之后,我尝试将缓冲区的str数组设置为OutputString

buffer[bufferFill].str = outputString;
这会产生以下错误:

编译时分配中的不兼容类型

我不知道怎么了。我以前做过类似的事情,没有任何问题,所以我不明白我做错了什么。我已经设置了item struct的其他属性,没有如下问题:

buffer[bufferFill].id = num;  // This gives no error
在设置
str
属性时,我也尝试过:

buffer[bufferFill].str = (char *)outputString;

我没想到这些会起作用,但我还是试过了。它们当然不起作用


据我所知,它们是相同类型的,我完全不知道是什么导致了这种情况。

使用strcpy()或strncpy()复制字符串(即字符数组)。

使用strcpy()或strncpy()复制字符串(即字符数组)。

使用strcpy()或strncpy()复制字符串(即字符数组).

使用strcpy()或strncpy()复制字符串(即字符数组)。

要复制字符串,请使用:-

strncpy(buffer[bufferFill].str,outputString,88); //88 is the size of char str[88] array.

要复制字符串,请使用:-

strncpy(buffer[bufferFill].str,outputString,88); //88 is the size of char str[88] array.

要复制字符串,请使用:-

strncpy(buffer[bufferFill].str,outputString,88); //88 is the size of char str[88] array.

要复制字符串,请使用:-

strncpy(buffer[bufferFill].str,outputString,88); //88 is the size of char str[88] array.

数组不可分配数组不可分配数组不可分配数组不可分配数组不可分配非常感谢!这就成功了。我确实想提醒大家,如果有人想在自己的问题上寻求帮助,应该是strncpy(buffer[bufferFill].str,outputString,88);因为两个数组的大小都是88,所以BUFFERSIZE=9。@Jason247:您是对的。它应该是88(即char str[]数组的大小),而不是BUFFERSIZE。我会编辑同样的。非常感谢!这就成功了。我确实想提醒大家,如果有人想在自己的问题上寻求帮助,应该是strncpy(buffer[bufferFill].str,outputString,88);因为两个数组的大小都是88,所以BUFFERSIZE=9。@Jason247:您是对的。它应该是88(即char str[]数组的大小),而不是BUFFERSIZE。我会编辑同样的。非常感谢!这就成功了。我确实想提醒大家,如果有人想在自己的问题上寻求帮助,应该是strncpy(buffer[bufferFill].str,outputString,88);因为两个数组的大小都是88,所以BUFFERSIZE=9。@Jason247:您是对的。它应该是88(即char str[]数组的大小),而不是BUFFERSIZE。我会编辑同样的。非常感谢!这就成功了。我确实想提醒大家,如果有人想在自己的问题上寻求帮助,应该是strncpy(buffer[bufferFill].str,outputString,88);因为两个数组的大小都是88,所以BUFFERSIZE=9。@Jason247:您是对的。它应该是88(即char str[]数组的大小),而不是BUFFERSIZE。将编辑相同的内容。