c - Segmentation fault when joining two programs
问题描述
I'm writing this program to evaluate post fix expressions and the program runs fine on its own. But when I join this program with another program of mine, it gives me segmentation fault.
Like this:
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!='\0')
{
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: %d\n", (exp->head1)->info);
return 0;
}
int main()
{
evaluationpost();
}
It will run fine and there won't be any errors. But once I type this code into another program and try to call the function, it gives me a "Segmentation fault".
This is the part of the code I'm trying to call the function evaluationpost in:
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 infix\n"); //and asking them what they want to do
printf("B- Convert it to prefix\n");
printf("C- Evaluate the expression\n");
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: %s\n", 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: %s\n", 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: %s\n", 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: %s\n", 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
}
When I run my program, type in a postfix expression and press C to evaluate it. It gives me "Segmentation Fault".
I don't understand why it's giving me a segmentation fault here when it works well on its own.
解决方案
这条线
scanf("%c", &option);
读入一个字符。但是当你输入时c<ENTER>
,你发送两个字符,第二个是换行符 \n
。
该gets()
函数(无论如何你都不应该使用它,听你的编译器!)
gets(input);
现在只读取(终止)换行符并存储一个空字符串(换行符被删除)。
while 条件现在立即为假,因此永远不会执行循环体:
while(*op!='\0')
现在当你取消引用
printf("Result: %d\n", (exp->head1)->info);
thenexp->head1
仍然是一个NULL
指针,由您的函数初始化init()
,导致分段错误。
您必须处理空字符串的条件并使用
scanf("%c ", &option);
吞下输入缓冲区中的换行符
推荐阅读
- c# - Application Insights 不显示 Trace.TraceInformation 消息
- c++ - MongoDB服务器与Mongo C++驱动的兼容性
- python-2.7 - SciPy/Numpy 的 Pooling/Convolution 比 Tensorflow 的 Convolution/Pooling 快?
- excel - 如何在excel中自动完成一个id_(数字)序列
- php - 在 google amp 上使用 ajax 加载更多结果
- ios - 迅速。导航控制器。有条件地改变子viewControllers的顺序
- java - 在java中缩放jpanel时缩放JPanel的组件
- protractor - 如何根据量角器中的条件退出方法执行?
- javascript - D3.js分层边缘捆绑按组着色
- android - Android Firebase 从 post 元素获取 Post id