该程序在Windows IDE中编译并运行良好,但赢得了';Linux-C语言中的t
我有一个计算后缀表达式的程序。当我从Windows IDE(代码块)编译和运行代码时,代码完全可以正常运行,没有编译器警告,但是,当我尝试在Linux环境中编译源代码时,会收到大量警告。它们列在下面:该程序在Windows IDE中编译并运行良好,但赢得了';Linux-C语言中的t,c,C,我有一个计算后缀表达式的程序。当我从Windows IDE(代码块)编译和运行代码时,代码完全可以正常运行,没有编译器警告,但是,当我尝试在Linux环境中编译源代码时,会收到大量警告。它们列在下面: postfix.c: In function ‘infixToPostfix’: postfix.c:20: warning: passing argument 1 of ‘stackInit’ from incompatible pointer type stack.h:25: note: ex
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”问题,我包含了头文件:
数学
所以它应该能够引用它。。。我不知道为什么它不会编译:/I我一直在用它来编译我的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;
}
/*将中缀转换为后缀的函数*/
char*infixToPostfix(char*infixStr)
{
静态字符pfline[30];
int i;
堆栈*s;
stackInit&s;
char*token=strtok(infixStr,“”);
对于(i=0;i<30;++i){
pfline[i]='\0';
}
while(令牌!=NULL)
{
如果(等标量(标记)!=0){
strcat(pfline、token);
strcat(pfline,“”);
}
if(isLeftParen(令牌))
堆栈推送(s&s,令牌);
if(等运算符(令牌))
{
如果(!stackIsEmpty(&s))
{
if(等参器(stackPeek&s)))
{
if(stackpreference(stackPeek&s))>=输入优先级(令牌))
strcat(pfline、stackPop和s);
strcat(pfline,“”);
}
}
堆栈推送(s&s,令牌);
}
如果(isRightParen(令牌))
{
而(!isLeftParen(stackPeek(&s)))
{
strcat(pfline、stackPop和s);
strcat(pfline,“”);
}
stackPop&s;
}
令牌=strtok(空,“”);
}
而(!stackIsEmpty(&s))
{
strcat(pfline、stackPop和s);
strcat(pfline,“”);
}
printf(“%s\n”,pfline);
stackDestroy&s;
返回线路;
}
int evaluatePostfix(char*postfix字符串)
{
堆栈*s;
int x=0,y=0,z=0;
stackInit&s;
char*token=strtok(postfixStr,“”);
while(令牌!=NULL)
{
如果(等标量(标记)!=0)
堆栈推送(s&s,令牌);
if(等运算符(令牌))
{
y=atoi(stackPop&s));
x=atoi(stackPop&s));
char*str=malloc(10*sizeof(char));
sprintf(str,“%d”,applyOperator(x,y,token));
stackPush&s,str;
}
令牌=strtok(空,“”);
}
z=atoi(stackPop&s));
stackDestroy&s;
返回z;
}
下面是我提供的stack.h头文件,它是堆栈的接口:
/*
* 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 */
/*
*这是一个字符串堆栈的接口。
*
*/
#ifndef\u堆栈\u H
#定义堆栈
#包括
typedef char*stkElement;
结构stkNode{
标准元素;
结构stkNode*下一步;
};
类型定义结构stkNode stkNode;
类型定义结构{
标准旋钮*顶部;
}堆叠;
/*函数初始化新的堆栈变量*/
void stackInit(堆栈*stkPtr);
/*函数以释放与堆栈关联的内存*/
无效堆栈销毁(堆栈*stkPtr);
/*函数将元素添加到堆栈顶部*/
无效堆栈推送(堆栈*stkPtr,stkElement元素);
/*从堆栈顶部移除元素的函数*/
标准件stackPop(stack*stkPtr);
/*函数,如果堆栈为空,则返回真值*/
bool stackIsEmpty(stack*stkPtr);
/*函数,该函数返回堆栈中的元素数*/
int stackLength(堆栈*stkPtr);
/*函数,该函数返回堆栈中的顶部元素而不删除它*/
标准件堆叠PEEK(堆叠*stkPtr);
#endif/*_堆栈_H*/
您没有发布相关代码,但为了让错误消息有意义,我必须假设s是指向堆栈的指针,stackInit使用指向堆栈的指针
在这种情况下,您应该将其称为stackInit(s)
,而将其称为stackInit(&s)
完全是错误的。如果您真的希望stackInit接受指向堆栈指针的指针,那么应该更改声明,这样说(但是我无法想象这会有什么意义)
也就是说,这里唯一的实际编译错误是“未定义对‘pow’的引用”,这与警告无关。通过使用gcc的
-lm
标志链接数学库,可以修复该错误。调用stackInit()
可能不是真正错误的,而是s
的声明。您需要使用stackInit(&s)是正确的
如果希望stackInit()
修改调用函数中的变量s
,但如果stackInit()
采用类型为stack*
的参数,则表明应将s
声明为:
stack s;
对于数学,您需要链接数学库(如果它没有像您的情况那样链接),请尝试
gcc prog2.c stack.c postfix.c-lm
。就警告而言,您传递的参数类型不正确。可能需要将s
声明为struct stack s代码>&然后调用stackInit(&s)
您应该添加-Wall
进行编译。显示stackInit&s的函数声明代码>,请把所有的符号都去掉