C语言 连接两个程序时出现分段错误



我正在编写这个程序来评估修复后的表达式,并且该程序自行运行良好。但是当我将该程序与我的另一个程序一起加入时,它给了我分段错误。

喜欢这个:

typedef struct node2
{
int info;
struct node2 *following;
}node2;
typedef struct eval
{
int size;
node2 *head1;
}eval;
int init(eval **exp)
{
*exp = (eval*)malloc(sizeof(eval));
if(*exp==NULL)
{
return EXIT_FAILURE;
}
(*exp)->head1 = NULL;
(*exp)->size = 0;
return EXIT_SUCCESS;
}
void postpush(eval *exp, int num)
{
node2 *newn=(node2*)malloc(sizeof(node2));
newn->following=exp->head1;
exp->head1=newn;
newn->info=num;
(exp->size)++;
}
void postpop(eval *exp)
{
node2 *postpop=NULL;
postpop = exp->head1;
exp->head1 = postpop->following;
(exp->size)--;
free(postpop);
}
int operand(char op)
{
if (op == '1'|| op == '2'||op == '3' || op == '4' || op == '5' || op == '6' || op == '7' || op == '8' || op == '9' || op == '0' )
return 1;
return 0;
}
void expre(int head2, int temp, char operand, eval* exp)
{
int result=0;
switch(operand)
{
case '+':
result=head2 + temp;
break;
case '-':
result=head2 - temp;
break;
case '/':
result=head2/temp;
break;
case '*':
result= head2*temp;
break;
case '^':
result= pow(head2,temp);
break;
default:
return;
}
postpop(exp);
postpush(exp,result);
}
int operators(char op)
{
if (op == '+' || op == '-' || op == '*' || op=='/' || op=='^')
return 1;
return 0;
}
int evaluationpost()
{
eval *exp = NULL;
char input[MAX];
init(&exp);
char* op;
printf("Please enter a postfix expression (with spaces between the operators and operands):");
gets(input);
int temp;
op=input;
while(*op!='')
{
if(operand(*op))
{
postpush(exp, abs((int)(*op)-48));
}
if (operators(*op))
{
temp=(exp->head1)->info;
postpop(exp);
expre((exp->head1)->info, temp, *op, exp);
}
op++;
}
printf("Result: %dn", (exp->head1)->info);
return 0;
}
int main()
{
evaluationpost();
}

它将运行良好,不会有任何错误。但是一旦我将此代码键入另一个程序并尝试调用该函数,它就会给我一个"分段错误"。

这是我尝试调用函数评估帖子的代码的一部分:

case 3: //if the expression is a postfix expression
printf("This is a postfix expression. Would you like to:n"); //telling the user its a postfix expression
printf("A- Convert it to infixn");                           //and asking them what they want to do
printf("B- Convert it to prefixn");
printf("C- Evaluate the expressionn");
printf("D- Exit the program.n");
printf("Your choice: "); //asking the user for their choice
scanf("%c", &option);    //getting the user's choice
if(option=='A' || option =='a') //if the user chooses to convert the expression to infix
{
printf("The infix expression is: %sn", ConvertPostfix(input, 2)); //calling the function that changes postfix to infix
     //and displaying the converted expression to the user
//saving the output in the output.txt file
output=ConvertPostfix(input, 2); //assigning the converted expression to output
fprintf(PTR, "Postfix expression converted to an infix expression: %sn", output); //putting the converted expression in the file
printf("Output was saved to output file.n"); //telling the user the output was saved in output.txt
fclose(PTR); //closing the file using its pointer
}
else if(option=='B' || option=='b') //if the user chooses to convert the expression to prefix
{
printf("The prefix expression is: %sn", ConvertPostfix(input, 1)); //calling the function that changes postfix to prefix
      //and displaying the converted expression to the user
//saving the output in the output.txt file
output=ConvertPostfix(input,1); //assigning the converted expression to output
fprintf(PTR, "Postfix expression converted to an prefix expression: %sn", output); //putting the converted expression in the file
printf("Output was saved to output file.n"); //telling the user the output was saved in output.txt
fclose(PTR); //closing the file using its pointer
}
else if(option=='C' || option =='c') //if the user chooses to evaluate the expression
{
evaluationpost(); //calling the function that evaluates postfix expressions
}

当我运行我的程序时,键入一个后缀表达式并按 C 来计算它。它给了我"分段错误"。

我不明白为什么当它本身运行良好时,它会在这里给我一个分段错误。

此行

scanf("%c", &option);

正好用一个字符读取。但是当你输入c<ENTER>时,你发送两个字符,第二个是换行n

gets()函数(无论如何你都不应该使用它,听听你的编译器!

gets(input);

现在只读取(终止(换行符并存储空字符串(换行符被删除(。

while 条件现在立即为 false,因此永远不会执行循环体:

while(*op!='')

现在,当您取消引用时

printf("Result: %dn", (exp->head1)->info);

然后exp->head1仍然是NULL指针,由init()函数初始化,导致分段错误。

您必须处理空字符串的条件并使用

scanf("%c ", &option);

吞下输入缓冲区中的换行

相关内容

  • 没有找到相关文章

最新更新