I am trying to write a program using code given to me by the author of my textbook, but I am getting “Dereferencing Pointer to Incomplete Type” errors in every method when trying to compile a program that uses the file. Below is the code. Does anyone know how I can fix this author’s code to get it to work?
#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];
}
Error:
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
Try squeezing in
typedef struct stackType *Stack;before the first function definition and see if that fixes it.