I have a program written to evaluate postfix expressions. I have the code fully functioning with no compiler warnings when I compile and run it from a Windows IDE (Codeblocks), however, when I try to compile the source code in a Linux environment, I get a ton or warnings. They are listed below:
postfix.c: In function ‘infixToPostfix’:
postfix.c:20: warning: passing argument 1 of ‘stackInit’ from incompatible pointer type
stack.h:25: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:36: warning: passing argument 1 of ‘stackPush’ from incompatible pointer type
stack.h:31: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:40: warning: passing argument 1 of ‘stackIsEmpty’ from incompatible pointer type
stack.h:37: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:42: warning: passing argument 1 of ‘stackPeek’ from incompatible pointer type
stack.h:43: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:44: warning: passing argument 1 of ‘stackPeek’ from incompatible pointer type
stack.h:43: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:45: warning: passing argument 1 of ‘stackPop’ from incompatible pointer type
stack.h:34: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:49: warning: passing argument 1 of ‘stackPush’ from incompatible pointer type
stack.h:31: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:54: warning: passing argument 1 of ‘stackPeek’ from incompatible pointer type
stack.h:43: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:56: warning: passing argument 1 of ‘stackPop’ from incompatible pointer type
stack.h:34: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:59: warning: passing argument 1 of ‘stackPop’ from incompatible pointer type
stack.h:34: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:63: warning: passing argument 1 of ‘stackIsEmpty’ from incompatible pointer type
stack.h:37: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:65: warning: passing argument 1 of ‘stackPop’ from incompatible pointer type
stack.h:34: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:69: warning: passing argument 1 of ‘stackDestroy’ from incompatible pointer type
stack.h:28: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c: In function ‘evaluatePostfix’:
postfix.c:139: warning: passing argument 1 of ‘stackInit’ from incompatible pointer type
stack.h:25: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:146: warning: passing argument 1 of ‘stackPush’ from incompatible pointer type
stack.h:31: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:150: warning: passing argument 1 of ‘stackPop’ from incompatible pointer type
stack.h:34: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:151: warning: passing argument 1 of ‘stackPop’ from incompatible pointer type
stack.h:34: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:154: warning: passing argument 1 of ‘stackPush’ from incompatible pointer type
stack.h:31: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:159: warning: passing argument 1 of ‘stackPop’ from incompatible pointer type
stack.h:34: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:160: warning: passing argument 1 of ‘stackDestroy’ from incompatible pointer type
stack.h:28: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
/tmp/ccPMgl0G.o: In function `applyOperator':
postfix.c:(.text+0x6bd): undefined reference to `pow'
collect2: ld returned 1 exit status
They all seem to be related to my postfix.c source and my stack.h header. The postfix.c source I completely modified myself, but the stack.h header was supplied by my instructor. All the errors for the postfix.c source seem to point to lines where I have code in the following manner:
stackInit(&s);
I believe it is referring to my use of the ampersand as a parameter for the function… but there isn’t any other way for me to indicate that I am modifying the immediate value of ‘s’ is there? Is there something I should be including before hand? Also… for the ‘pow’ issue, I have included the header file:
math.h
So it should be able to reference it… I don’t know why it won’t compile :/ I have been using this to compile my 3 source files together:
gcc prog2.c stack.c postfix.c
Is there another way I should be doing this? Thank you in advance.
Source Code:
/* function to convert an infix to postfix */
char *infixToPostfix(char *infixStr)
{
static char pfline[30];
int i;
stack * s;
stackInit(&s);
char * token = strtok(infixStr, " ");
for(i = 0; i < 30; ++i) {
pfline[i] = '\0';
}
while(token != NULL)
{
if(isOperand(token) != 0) {
strcat(pfline, token);
strcat(pfline, " ");
}
if(isLeftParen(token))
stackPush(&s, token);
if(isOperator(token))
{
if(!stackIsEmpty(&s))
{
if(isOperator(stackPeek(&s)))
{
if(stackPrecedence(stackPeek(&s)) >= inputPrecedence(token))
strcat(pfline, stackPop(&s));
strcat(pfline, " ");
}
}
stackPush(&s, token);
}
if(isRightParen(token))
{
while(!isLeftParen(stackPeek(&s)))
{
strcat(pfline, stackPop(&s));
strcat(pfline, " ");
}
stackPop(&s);
}
token = strtok(NULL, " ");
}
while(!stackIsEmpty(&s))
{
strcat(pfline, stackPop(&s));
strcat(pfline, " ");
}
printf("%s\n", pfline);
stackDestroy(&s);
return pfline;
}
int evaluatePostfix(char *postfixStr)
{
stack * s;
int x = 0, y = 0, z = 0;
stackInit(&s);
char * token = strtok(postfixStr, " ");
while(token != NULL)
{
if(isOperand(token) != 0)
stackPush(&s, token);
if(isOperator(token))
{
y = atoi(stackPop(&s));
x = atoi(stackPop(&s));
char *str = malloc(10 * sizeof(char));
sprintf(str, "%d", applyOperator(x, y, token));
stackPush(&s, str);
}
token = strtok(NULL, " ");
}
z = atoi(stackPop(&s));
stackDestroy(&s);
return z;
}
Here is my stack.h header file that was provided which is an interface for the stack:
/*
* This is an interface for a stack of strings.
*
*/
#ifndef _STACK_H
#define _STACK_H
#include <stdbool.h>
typedef char * stkElement;
struct stkNode {
stkElement element;
struct stkNode *next;
};
typedef struct stkNode stkNode;
typedef struct {
stkNode *top;
} stack;
/* function to initialize a new stack variable */
void stackInit(stack *stkPtr);
/* function to free the memory associated with the stack */
void stackDestroy(stack *stkPtr);
/* function to add an element to the top of the stack */
void stackPush(stack *stkPtr, stkElement element);
/* function that removes the element from the top of the stack */
stkElement stackPop(stack *stkPtr);
/* function that returns a true value if the stack is empty */
bool stackIsEmpty(stack *stkPtr);
/* function that returns the number of elements in the stack */
int stackLength(stack *stkPtr);
/* function that returns the top element in the stack without removing it */
stkElement stackPeek(stack *stkPtr);
#endif /* _STACK_H */
It is likely not the call to
stackInit()that is actually wrong – rather it is the declaration ofs. You are correct that you need to usestackInit(&s);if you wantstackInit()to modify the variablesin the calling function, but ifstackInit()takes a parameter of typestack *then that indicates that you should be declaringsas: