如下代码所示,关于malloc与realloc出现5处错误:
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;
typedef struct{
int *base;
int *top;
int stacksize;
}SqStack;
void InitStack(SqStack S){
S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
}//___________________InitStack_______________________
void Push(SqStack S,int e){
if(S.top==S.base){
S.base=(int *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(int));
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top=e;
S.top++;
}
void main(){
SqStack S;
InitStack(S);
Push (S,5);
}
SqStack.c
D:\VC6\pvc6\COMMON\MSDEV98\BIN\SqStack.c(13) : error C2143: syntax error : missing ')' before ';'
D:\VC6\pvc6\COMMON\MSDEV98\BIN\SqStack.c(13) : error C2059: syntax error : ')'
D:\VC6\pvc6\COMMON\MSDEV98\BIN\SqStack.c(13) : error C2100: illegal indirection
D:\VC6\pvc6\COMMON\MSDEV98\BIN\SqStack.c(20) : error C2143: syntax error : missing ')' before ';'
D:\VC6\pvc6\COMMON\MSDEV98\BIN\SqStack.c(20) : error C2143: syntax error : missing ';' before ')'
Error executing cl.exe.
SqStack.obj - 5 error(s), 0 warning(s)
求大佬解答~~
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
宏定义不要加分号在后面
我改了一下你的程序,运行都没问题,但是这个顺序栈的读起来有点费尽,我搜了一下和你写法相近的,
建议看一下https://blog.csdn.net/lady_killer9/article/details/82712668
#include
#include
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct {
int* base;
int* top;
int stacksize;
}SqStack;
void InitStack(SqStack* S) {
S->base = (int*)malloc((STACK_INIT_SIZE) * sizeof(int));
S->top = S->base;
S->stacksize = STACK_INIT_SIZE;
}//___________________InitStack_______________________
void Push(SqStack* S, int e) {
if (S->top == S->base) {
S->base = (int*)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(int));
S->top = S->base + S->stacksize;
S->stacksize += STACKINCREMENT;
}
*S->top=e;
S->top--;
}
int main() {
SqStack S;
InitStack(&S);
Push(&S, 5);
return 0;
}