I have been trying to implement a stack on dev c with c , this is my code
#include <stdio.h>
#include <stdlib.h>
#define stackinitsize 100
#define stackincrement 10
typedef struct {
char *base;
char *top;
int stacksize;
}sqstack,*s;
Status initstack(sqstack &s)
{
s.base=(char *)malloc(stackinitsize*sizeof(char));
if(!s.base) exit(OVERFLOW);
s.top=s.base;
s.stacksize=stackinitsize;
return OK;
}
void push(sqstack &s,char e)
{
if ((s.top-s.base)>=s.stacksize){
s.base=(char *)realloc(s.base,(s.stacksize+stackincrement)*sizeof(char));
if(!s.base) exit (OVERFLOW);
s.top=s.base+s.stacksize;
s.stacksize+=stackincrement;
}
*s.top++=e;
}
int main(int argc, char *argv[])
{
char e;
system("PAUSE");
return 0;
}
but it keeps on saying
error: syntax error before "s"
error: syntax error before '&' token
but when I put it and compile it on codeblocks it says
error: expected ';', ',' or ')' before '&' token
Does someone have an idea?
You appear to be using passing by reference which is a C++ feature, not C.
You cannot use
&in declaration in C, when you want to declare that a pointer is passed you use*(same goes for thepushfunction):Note, however, that unlike C++ pass-by-reference, you cannot change the argument passed to the function outside of the function by assignment to
s, only to dereference it.