简体   繁体   English

C-编译器错误:取消引用不完整类型的指针

[英]C - compiler error: dereferencing pointer to incomplete type

I've seen many questions here about dereferencing pointers to incomplete types but every single one of them is related to not using typedef or to having the structs declared in the .c, not in the header file. 我在这里看到了很多有关将指针取消引用不完整类型的问题,但是其中的每一个都与不使用typedef或在.c中(而不是在头文件中)声明结构有关。 I've been trying to fix this for many hours and can't seem to find a way. 我已经尝试修复了多个小时,似乎找不到办法。

stable.h (cannot be changed): stable.h(无法更改):

typedef struct stable_s *SymbolTable;

typedef union {
    int i;
    char *str;
    void *p;
} EntryData;

SymbolTable stable_create();

stable.c: stable.c:

SymbolTable stable_create() {
    SymbolTable ht = malloc(sizeof (SymbolTable));
    ht->data = malloc(primes[0] * sizeof(Node));
    for (int h = 0; h < primes[0]; h++) ht->data[h] = NULL;
    ht->n = 0;
    ht->prIndex = 0;
    return ht;
}

aux.h: aux.h:

#include "stable.h"

typedef struct {
    EntryData *data;
    char *str;
    void *nxt;
} Node;


typedef struct {
    Node **data;
    int n;
    int prIndex;
} stable_s;

typedef struct {
    char **str;
    int *val;
    int index;
    int maxLen;
} answer;

freq.c: freq.c:

answer *final;
static void init(SymbolTable table){
    final = malloc(sizeof(answer));
    final->val = malloc(table->n * sizeof(int));
}

int main(int argc, char *argv[]) {
    SymbolTable st = stable_create();
    init(st);
}

compiler error (using flags -Wall -std=c99 -pedantic -O2 -Wextra): 编译器错误 (使用标志-Wall -std = c99 -pedantic -O2 -Wextra):

freq.c:13:30: error: dereferencing pointer to incomplete type ‘struct stable_s’
 final->val = malloc(table->n * sizeof(int));

This code 这段代码

 typedef struct stable_s *SymbolTable;

defines the type SymbolTable as a pointer to struct stable_s . SymbolTable类型定义为指向struct stable_s的指针。

This code 这段代码

typedef struct {
    Node **data;
    int n;
    int prIndex;
} stable_s;

defines a structure of type stable_s . 定义类型为stable_s的结构。 Note that stable_s is not struct stable_s . 请注意, stable_s不是struct stable_s

A simple 一个简单的

struct stable_s {
    Node **data;
    int n;
    int prIndex;
};

without the typedef will solve your problem. 没有typedef可以解决您的问题。

See C : typedef struct name {...}; 参见C:typedef结构名称{...}; VS typedef struct{...} name; VS typedef struct {...}名称;

As Andrew points out, declaring a "struct stable_s { ... }" will make things compile. 正如安德鲁指出的那样,声明“ struct stable_s {...}”将使事情得以编译。

However, you don't say whether this is a class assignment or real-world. 但是,您不会说这是班级作业还是实际作业。 If real-world, it's probably an extremely bad idea to declare the struct yourself. 如果是真实世界,那么自己声明该结构可能是一个非常糟糕的主意。 You are being given an opaque type to use to reference a library; 您将获得一个不透明的类型以用于引用库。 you are not supposed to know or access the stuff inside. 您不应该了解或访问其中的内容。 The library is relying on various semantics that you might mess up, and as software versions change, the contents of the struct can (and almost certainly will) change, so your code will break in the future. 该库依赖于您可能会弄乱的各种语义,并且随着软件版本的更改,该结构的内容可以(并且几乎肯定会)更改,因此您的代码将来会中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM