![](/img/trans.png)
[英]compiler error - dereferencing pointer to incomplete type In C programming
[英]C - compiler error: dereferencing pointer to incomplete type
我在這里看到了很多有關將指針取消引用不完整類型的問題,但是其中的每一個都與不使用typedef或在.c中(而不是在頭文件中)聲明結構有關。 我已經嘗試修復了多個小時,似乎找不到辦法。
stable.h(無法更改):
typedef struct stable_s *SymbolTable;
typedef union {
int i;
char *str;
void *p;
} EntryData;
SymbolTable stable_create();
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:
#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:
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);
}
編譯器錯誤 (使用標志-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));
這段代碼
typedef struct stable_s *SymbolTable;
將SymbolTable
類型定義為指向struct stable_s
的指針。
這段代碼
typedef struct {
Node **data;
int n;
int prIndex;
} stable_s;
定義類型為stable_s
的結構。 請注意, stable_s
不是struct stable_s
。
一個簡單的
struct stable_s {
Node **data;
int n;
int prIndex;
};
沒有typedef
可以解決您的問題。
正如安德魯指出的那樣,聲明“ struct stable_s {...}”將使事情得以編譯。
但是,您不會說這是班級作業還是實際作業。 如果是真實世界,那么自己聲明該結構可能是一個非常糟糕的主意。 您將獲得一個不透明的類型以用於引用庫。 您不應該了解或訪問其中的內容。 該庫依賴於您可能會弄亂的各種語義,並且隨着軟件版本的更改,該結構的內容可以(並且幾乎肯定會)更改,因此您的代碼將來會中斷。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.