0

Please find below my function in C. I operate there using a stack which is part of another file, but that is working correctly.

void doOperation ( tStack* s, char c, char* postExpr, unsigned* postLen ) {
if ( ( c == ( '*' || '\' ) ) && ( s->arr[s->top] == ( '+' || '-' ) ) ) 
    stackPush( s, c); 
else if ( c == ( '+' || '-' ) && s->arr[s->top] == ( '*' || '/' ) ) {
    stackTop( s, postExpr[postLen] );
    *(postLen)++;
    stackPop( s );  
    stackPush( s, c);
}
else if ( c == '(' ) 
    stackPush( s, c);
else if ( c == ')' ) 
    untilLeftPar( s, postExpr, postLen);
else {
    stackTop( s, postExpr[postLen] );
    *(postLen)++;
    stackPop( s );  
    stackPush( s, c);
}

}

I get these errors and I have no idea what is wrong:

c204.c:70:23: warning: character constant too long for its type [enabled by default]
c204.c:70:58: warning: multi-character character constant [-Wmultichar]
c204.c:70:65: warning: missing terminating ' character [enabled by default]
c204.c:70:2: error: missing terminating ' character
c204.c:71:3: error: void value not ignored as it ought to be
c204.c:71:19: error: expected ‘)’ before ‘;’ token
c204.c:88:1: error: expected ‘)’ before ‘}’ token
c204.c:88:1: error: expected ‘)’ before ‘}’ token
c204.c:88:1: error: expected expression before ‘}’ token
../c202/c202.c: In function ‘stackTop’:
../c202/c202.c:100:18: warning: the comparison will always 
evaluate as ‘true’ for the address of ‘stackEmpty’ will never be NULL [-Waddress]
../c202/c202.c: In function ‘stackPop’:
../c202/c202.c:120:18: warning: the comparison will always 
evaluate as ‘true’ for the  address of ‘stackEmpty’ will never be NULL     [-Waddress]
../c202/c202.c: In function ‘stackPush’:
../c202/c202.c:133:17: warning: the comparison will always 
evaluate as ‘false’ for the address of ‘stackFull’ will never be NULL [-Waddress]
make: *** [c204-test] Error 1

What might be the cause of these errors?

Bart
  • 19,692
  • 7
  • 68
  • 77
palci12
  • 179
  • 1
  • 3
  • 12
  • I don't know what you're using to edit your code, but in this case the syntax highlighting is a pretty decent indication of where things go wrong. And don't skip items in your compiler errors. You're focusing on the expected token error, where the first actual error (and warnings before it) are actually a better indicator of the problem. – Bart Oct 23 '13 at 14:09
  • Even the syntax-highlighting on StackOverflow catches it ... – wildplasser Oct 23 '13 at 14:14

1 Answers1

9

You need to read about escape sequences, this '\' is the problem in the line:

 if ( ( c == ( '*' || '\' ) ) && ( s->arr[s->top] == ( '+' || '-' ) ) ) 
stackPush( s, c); 

Replace it with '\\':

if ( ( c == '*' || c == '\\' )  && ( ( s->arr[s->top] ==  '+' || s->arr[s->top] == '-' ) ) ) 
stackPush( s, c); 

\\ is for Backslash. For more read this answer. Also the condition should not be written as ( c == ( '*' || '\\' ) ) - it should be c == '*' || c == '\\'

The else if portion is also slightly messed up, it should be something like:

else if ( ( c == '+' || c == '-' ) && ( s->arr[s->top] == '*' || s->arr[s->top] == '/' ) ) 

Also not sure if here s->arr[s->top] == '/' the '/' was a typo or not but if in case you needed backslash instead of '/' you will again have to use this '\\'.

Community
  • 1
  • 1
Sadique
  • 22,572
  • 7
  • 65
  • 91