####问题发生背景:数据结构顺序栈的初始化失败
####错误信息:S->base=(SElemType)malloc(MAXSIZEsizeof(SElemType));
```c
typedef struct
{
int example;
}SElemType;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
int main()
{
Status InitStack(SqStack *S);
SqStack *S;
InitStack(S);
printf("%o",S->base);
}
Status InitStack(SqStack *S)
{
S->base=(*SElemType)malloc(MAXSIZE*sizeof(SElemType));
if(!S->base) exit(OVERFLOW);
S->top=S->base;
S->stacksize=MAXSIZE;
return OK;
}
详细报错:[Error] expected expression before 'SElemType'
尝试方法:通过调试定位报错语句,最后发现是S->base编译系统无法理解,但S->表示指针所指向的结构体成员变量,语法上是可行的,百思不得其解。
###希望各位掌握相关知识的友友不吝赐教
改动处见注释,供参考:
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 50 //修改
#define OVERFLOW -1 //修改
#define OK 1 //修改
typedef int Status; //修改
typedef struct{
int example;
}SElemType;
typedef struct{
SElemType* base;
SElemType* top;
int stacksize;
}SqStack;
int main()
{
Status InitStack(SqStack * S);
SqStack* S;
S = (SqStack*)malloc(sizeof(SqStack)); //修改
InitStack(S);
printf("%o", S->base);
return 0;
}
Status InitStack(SqStack* S)
{
S->base = (SElemType*)malloc(MAXSIZE * sizeof(SElemType)); //修改
//S->base = (*SElemType)malloc(MAXSIZE * sizeof(SElemType));
if (!S->base)
exit(OVERFLOW);
S->top = S->base;
S->stacksize = MAXSIZE;
return OK;
}
Status InitStack(SqStack *S); 这个函数声明没移到main函数前面?