c - 赋值时类型不兼容

标签 c pointers

我有以下代码,我读了很多遍,它总是看起来没问题,但编译器说:

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/

相关文章:

c - 为什么管道会脱离环路?

c++ - 在用 C++ 编译的 C 库中如何处理运行时错误?

c - 将数据分配给数组

c - 在 C 中将一个函数内联到另一个内联函数中

c# - 如何使用 size_t 遍历 uint64*,c# 的等价物是什么?

c - 用C语言解析单词;翻译程序

c - 如何使用 memcpy 将指针复制到数组元素

c++ - 访问冲突 OpenCV C++ dll

将 char 指针转换为 int 指针 - 缓冲区错误 10

iphone - Objective-C 指针 > 指向属性