c: 不透明数据类型创建, "invalid application of ‘sizeof’ 到不完整类型”

标签 c

我试图隐藏我的结构的实现。该结构的定义位于 Board.h 中:

typedef struct Board* BoardP;
typedef const struct Board* ConstBoardP;

我想在 Board.c 中根据需要分配内存:

#include <stdio.h>
#include <stdlib.h>
#include "Board.h"
#define TARGET_LENGTH 5
#define DEFAULT_BOARD_SIZE 10

 struct Board*
 newBoard(int r, int c)
{
    struct Board *b = (struct Board*) malloc(sizeof(struct Board));

     char ** array;
     int i;
     array = (char**) malloc( r*sizeof(char*) );
     for (i=0; i<r; i++)
     {
        array[i] = malloc( c*sizeof(char) );
     }
     b->_values = array;
     b->_last_player = 'X';
     b->_size_r = r;
     b->_size_c = c;
    return b;
}

但是我得到了错误:

invalid application of ‘sizeof’ to incomplete type ‘struct Board’

enter image description here

我现在在原地转了几个小时,需要有人帮我理清我造成的困惑局面。如果我想动态地将内存分配给结构内的数组,我该如何预先定义结构?

最佳答案

您必须先在 Board.c定义 Board 结构:

//Board.c
struct Board {
 ....
};

或者在 Board.c 中被 #include 的其他地方,无论如何,编译器在使用 sizeof 时需要查看它的定义来确定它的大小() 并访问其成员。 Wikipedia Page 中有一个示例也是。

关于c: 不透明数据类型创建, "invalid application of ‘sizeof’ 到不完整类型”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13627455/

相关文章:

c - 头文件中的枚举类型和结构

c - Misra C 规则 12.2 - 误报警告?

c - 二分查找 - 如果我从列表中间添加或减去 "1",我会得到中间数字左边的第一个数字还是一个数值?

c++ - 捕获自定义 UART 类信号

c - 此 C 代码片段背后的安全考虑

c - 如何通过 VS2012 使用 C 编码使文本文件内容出现在命令提示符上

C++ 获取在 Linux 后台按下的键

c - 插入链表

mmap可以通过函数传递地址吗?

c - 对 C 中的指针感到困惑