我有以下代码,我读了很多遍,它总是看起来没问题,但编译器说:
error: incompatible types when assigning to type 'struct Type[10]' from type 'Type'
这是代码:
#include <stdio.h>
typedef struct
{
int a;
int b;
} Type;
typedef struct
{
Type (*typeArray)[10];
} Type2;
void someFunction(Type2 *type2Variable, Type (*typeArray)[10])
{
Type typeNewValue;
type2Variable->typeArray[0] = typeNewValue; /* This throws the error */
}
int main(int argc, char **argv)
{
Type typeArray[10];
Type2 type2Variable;
someFunction(&type2Variable, &typeArray);
}
我确信解决方案只是某个地方的 *
但我找不到它。
更新
非常感谢您的回答,这只是一个毫无意义的示例来展示我的问题(这不是真正的代码)。
解决方案是:
(*type2Variable->typeArray)[0] = typeNewValue;
而不是:
type2Variable->typeArray[0] = typeNewValue;
最佳答案
Type (*typeArray)[10]
声明一个指向 Type
类型、大小为 10 的数组的指针。看来您想要声明一个 Type
数组:
typedef struct
{
Type typeArray[10];
} Type2;
void someFunction(Type2 *type2Variable, Type * typeArray) { /* ... */ }
关于c - 赋值时类型不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24478842/