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. I've been trying to fix this for many hours and can't seem to find a way.
stable.h (cannot be changed):
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);
}
compiler error (using flags -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
.
This code
typedef struct {
Node **data;
int n;
int prIndex;
} stable_s;
defines a structure of type stable_s
. Note that stable_s
is not struct stable_s
.
A simple
struct stable_s {
Node **data;
int n;
int prIndex;
};
without the typedef
will solve your problem.
See C : typedef struct name {...}; VS typedef struct{...} name;