我有一个编写程序来评估后缀表达式。当我从Windows IDE(Codeblocks)编译并运行它时,我的代码完全运行,没有编译器警告,但是,当我尝试在Linux环境中编译源代码时,出现一些警告或警告。他们列在下面:
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它们似乎都与我的postfix.c源代码和我的stack.h头文件有关。 postfix.c源代码完全修改了自己,但是stack.h头文件由我的教师提供。 postfix.c源代码的所有错误似乎都指向我以下列方式编写代码的行:
stackInit(&s);
我相信这是指我使用&符号作为函数的参数......但是我没有任何其他方法可以表明我正在修改's'的即时值吗?有什么我应该包括在手前?另外...对于'pow'问题,我已经包含头文件:
math.h
所以它应该能够引用它......我不知道为什么它不会编译:/我一直在使用它来一起编译我的3个源文件:
gcc prog2.c stack.c postfix.c
还有另外一种方法我应该这样做吗?先谢谢你。
源代码:
/* 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;
}这是我提供的stack.h头文件,它是堆栈的接口:
/*
* This is an interface for a stack of strings.
*
*/
#ifndef _STACK_H
#define _STACK_H
#include
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 */