c - 提供的代码中的 "Dereferencing Pointer to Incomplete Type"

标签 c compiler-errors stack

我正在尝试使用教科书作者给我的代码编写一个程序,但在尝试编译使用该文件的程序时,每个方法都会出现“取消引用指向不完整类型的指针”错误。下面是代码。有谁知道如何修复该作者的代码以使其正常工作?

#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/

相关文章:

ios - Objective-C语法问题

c - 将 char 数组拆分为分隔符为 NUL char 的标记

c - C 中 printf 的背后是什么?

arrays - 在 Julia 中按列堆叠数组

c++ - 循环语句的困惑

java - Kotlin:javaSetter 方法未编译

visual-studio - 构建 :Generic type 'Promise<T, R>' requires 2 type argument(s)

c++ - 关于运行良好的堆栈 RPN 程序上的 char/int

python - 在 Python 中反转堆栈

asp.net-mvc - 没有类型为 'IEnumerable<SelectListItem>' 的 ViewData 项具有 key 'SubGoods'