【发布时间】:2020-12-15 19:25:07
【问题描述】:
我正在尝试使用堆栈实现括号平衡检查器,但我似乎无法摆脱这个
错误
tempCodeRunnerFile.c: In function ‘main’:
tempCodeRunnerFile.c:26:20: error: dereferencing pointer to incomplete type ‘struct StackRecord’
26 | S = malloc(sizeof(*S));
|
代码如下:
balance.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "stack.h"
void main()
{
struct StackRecord *S;
char str[500], c;
int l, i;
S = malloc(sizeof(*S));
while (1) {
.............
.............
return;
}
stack.c
#include "stack.h"
#include "fatal.h"
#include <stdlib.h>
#define EmptyTOS ( -1 )
#define MinStackSize ( 5 )
struct StackRecord
{
int Capacity;
int TopOfStack;
ElementType *Array;
};
.............
.............
stack.h
typedef int ElementType;
/* START: fig3_45.txt */
#ifndef _Stack_h
#define _Stack_h
struct StackRecord;
typedef struct StackRecord *Stack;
int IsEmpty( Stack S );
int IsFull( Stack S );
Stack CreateStack( int MaxElements );
void DisposeStack( Stack S );
void MakeEmpty( Stack S );
void Push( ElementType X, Stack S );
ElementType Top( Stack S );
void Pop( Stack S );
ElementType TopAndPop( Stack S );
#endif /* _Stack_h */
/* END */
我只列出了导致问题的重要部分。这是没有意义的,因为一切似乎都是正确的:/
【问题讨论】:
-
如果你想创建一个不透明的数据结构,那么你需要实现自己的工厂函数来创建数据结构的实例。
-
您应该阅读 Modern C,您的 C 编译器(例如 GCC...)和调试器(例如 GDB)的文档。如果您使用 GCC,请使用所有警告和调试信息进行编译,例如
gcc -Wall -Wextra -g。你可能想要一些flexible array member
标签: c struct compiler-errors declaration definition