我正在尝试使用教科书作者给我的代码编写一个程序,但在尝试编译使用该文件的程序时,每个方法都会出现“取消引用指向不完整类型的指针”错误。下面是代码。有谁知道如何修复该作者的代码以使其正常工作?
#include <stdio.h>
#include <stdlib.h>
#include "StackADT.h"
#define STACK_SIZE 100
struct stackType {
int contents[STACK_SIZE];
int top;
};
static void terminate(const char *message) {
printf("%s\n", message);
exit(EXIT_FAILURE);
}
Stack create(void) {
Stack s = malloc(sizeof(struct stackType));
if (s == NULL) {
terminate("Error: Stack could not be created");
}
s->top = 0;
return s;
}
void destroy(Stack s) {
free(s);
}
void makeEmpty(Stack s) {
s->top = 0;
}
bool isEmpty(Stack s) {
return s->top == 0;
}
bool isFull(Stack s) {
return s->top == STACK_SIZE;
}
void push(Stack s, Item i) {
if (isFull(s)) {
terminate("Error: Stack is full");
}
s->contents[s->top++] = i;
}
int pop(Stack s) {
if (isEmpty(s)) {
terminate("Error: Stack is empty");
}
return s->contents[--s->top];
}
错误:
StackADT.c: In function 'create':
StackADT.c:29: error: dereferencing pointer to incomplete type
StackADT.c: In function 'makeEmpty':
StackADT.c:38: error: dereferencing pointer to incomplete type
StackADT.c: In function 'isEmpty':
StackADT.c:42: error: dereferencing pointer to incomplete type
StackADT.c: In function 'isFull':
StackADT.c:46: error: dereferencing pointer to incomplete type
StackADT.c: In function 'push':
StackADT.c:54: error: dereferencing pointer to incomplete type
StackADT.c:54: error: dereferencing pointer to incomplete type
StackADT.c: In function 'pop':
StackADT.c:62: error: dereferencing pointer to incomplete type
StackADT.c:62: error: dereferencing pointer to incomplete type
最佳答案
尝试在第一个函数定义之前压缩 typedef struct stackType *Stack;
并查看是否可以修复该问题。
关于c - 提供的代码中的 "Dereferencing Pointer to Incomplete Type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13548848/