为什么 strlen 在 C 中导致分段错误?
Why is strlen causing a segmentation fault in C?
(警告)是的,这是我正在处理的任务的一部分,但此时我完全绝望了,不,我不是在找你们帮我解决这个问题,但任何提示都会很重要赞赏!(/警告)
我非常想制作一个交互式菜单,用户应该输入一个表达式(例如“5 3 +”)并且程序应该检测到它是后缀表示法,不幸的是我一直在进行分段故障错误,我怀疑它们与 strlen 函数的使用有关。
EDIT: I was able to make it work, first the char expression[25] = {NULL};
line
becomes char expression[25] = {'[=12=]'};
And when calling the determine_notation
function I removed the [25]
from the array I am passing like so:
determine_notation(expression, expr_length);
Also the input[length]
part I changed to input[length-2]
since like mentioned in a previous comment, input[length] == '[=18=]'
and input[length--] == '\n'
.
All in all thanks for all the help!
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int determine_notation(char input[25], int length);
int main(void)
{
char expression[25] = {NULL}; // Initializing character array to NULL
int notation;
int expr_length;
printf("Please enter your expression to detect and convert it's notation: ");
fgets( expression, 25, stdin );
expr_length = strlen(expression[25]); // Determining size of array input until the NULL terminator
notation = determine_notation( expression[25], expr_length );
printf("%d\n", notation);
}
int determine_notation(char input[25], int length) // Determines notation
{
if(isdigit(input[0]) == 0)
{
printf("This is a prefix expression\n");
return 0;
}
else if(isdigit(input[length]) == 0)
{
printf("This is a postfix expression\n");
return 1;
}
else
{
printf("This is an infix expression\n");
return 2;
}
}
您可能收到一条警告,说明您正在将 char
转换为此调用中的指针:
expr_length = strlen(expression[25]);
// ^^^^
这就是问题所在 - 您的代码在引用数组末尾之后的不存在元素(未定义的行为)并试图将其传递给 strlen
。
由于 strlen
接受指向字符串开头的指针,因此调用需要
expr_length = strlen(expression); // Determining size of array input until the NULL terminator
(警告)是的,这是我正在处理的任务的一部分,但此时我完全绝望了,不,我不是在找你们帮我解决这个问题,但任何提示都会很重要赞赏!(/警告)
我非常想制作一个交互式菜单,用户应该输入一个表达式(例如“5 3 +”)并且程序应该检测到它是后缀表示法,不幸的是我一直在进行分段故障错误,我怀疑它们与 strlen 函数的使用有关。
EDIT: I was able to make it work, first the
char expression[25] = {NULL};
line
becomeschar expression[25] = {'[=12=]'};
And when calling the
determine_notation
function I removed the[25]
from the array I am passing like so:determine_notation(expression, expr_length);
Also the
input[length]
part I changed toinput[length-2]
since like mentioned in a previous comment,input[length] == '[=18=]'
andinput[length--] == '\n'
.All in all thanks for all the help!
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int determine_notation(char input[25], int length);
int main(void)
{
char expression[25] = {NULL}; // Initializing character array to NULL
int notation;
int expr_length;
printf("Please enter your expression to detect and convert it's notation: ");
fgets( expression, 25, stdin );
expr_length = strlen(expression[25]); // Determining size of array input until the NULL terminator
notation = determine_notation( expression[25], expr_length );
printf("%d\n", notation);
}
int determine_notation(char input[25], int length) // Determines notation
{
if(isdigit(input[0]) == 0)
{
printf("This is a prefix expression\n");
return 0;
}
else if(isdigit(input[length]) == 0)
{
printf("This is a postfix expression\n");
return 1;
}
else
{
printf("This is an infix expression\n");
return 2;
}
}
您可能收到一条警告,说明您正在将 char
转换为此调用中的指针:
expr_length = strlen(expression[25]);
// ^^^^
这就是问题所在 - 您的代码在引用数组末尾之后的不存在元素(未定义的行为)并试图将其传递给 strlen
。
由于 strlen
接受指向字符串开头的指针,因此调用需要
expr_length = strlen(expression); // Determining size of array input until the NULL terminator