gpt4 book ai didi

c - 我的 C 代码中的错误 "segmentation fault"

转载 作者:太空狗 更新时间:2023-10-29 15:52:51 24 4
gpt4 key购买 nike

#include<string.h>
#include<stdio.h>


int firstState(char s[], int length);
int secondState(char s[], int length);
int thirdState(char s[], int length);
int forthState(char s[], int length);

int main()
{
char string[10];

gets(string);

if( firstState(string, 0) )
printf("Accept\n");
else
printf( "Not accept\n" );

return 0;
}

int firstState(char s[], int length)
{
if(s[length] == 'a')
return (secondState(s, length++));
else if(s[length] == 'b')
return firstState(s, length++);
else
return 0;
}

int secondState(char s[], int length)
{
if(s[length] == 'a')
return secondState(s, length++);
else if(s[length] == 'b')
return thirdState(s, length++);
else
return 0;
}

int thirdState(char s[], int length)
{
if(s[length] == 'a')
return secondState(s, length++);
else if(s[length] == 'b')
return forthState(s, length++);
else
return 0;
}

int forthState(char s[], int length)
{
if(s[length] == 'a')
return secondState(s, length++);
else if(s[length] == 'b')
return firstState(s, length++);
else
return 0;
}

它给了我一个段错误或核心转储,我很困惑!!!有人可以解释为什么它给了我这种错误吗????并告诉我如何调试才能让我的代码运行的非常清晰!!

我真的厌倦了这个:(

抱歉我的英语不好

最佳答案

你有一个无限递归,

return (secondState(s, length++));

传递的 length 参数是递增前 length 的值,因此您只需查看第一个 char

length 参数作为 length + 1 传递,并检查 length 是否小于 10(char 的长度 数组 string).

另外,

gets(string);

是非常不安全的,如果输入超过九个字符,你就写在分配的内存之外。使用

fgets(string, sizeof string, stdin);

相反。


嗯,由于只需要上述修复和改变一个返回值,所以大部分逻辑是正确的,修复代码:

// #include<string.h> <- We don't use that
#include<stdio.h>

// Match the grammar (a+b)*abb

int firstState(char s[], int length); // nothing of the suffix matched
int secondState(char s[], int length); // matched one character of the suffix
int thirdState(char s[], int length); // matched two
int forthState(char s[], int length); // matched the complete suffix

int main()
{
char string[10];
// Get a 0-terminated string into the buffer.
fgets(string, sizeof string, stdin);

if( firstState(string, 0) )
printf("Accept\n");
else
printf( "Not accept\n" );

return 0;
}

int firstState(char s[], int length)
{
if(s[length] == 'a') // first character of suffix matched
return (secondState(s, length+1));
else if(s[length] == 'b') // nothing matched
return firstState(s, length+1);
else // end of string in not-accepting state
return 0;
}

int secondState(char s[], int length)
{
if(s[length] == 'a') // the old matched 'a' wasn't part of the suffix, the new may be
return secondState(s, length+1);
else if(s[length] == 'b') // now matched two characters of the suffix
return thirdState(s, length+1);
else // end of string in not-accepting state
return 0;
}

int thirdState(char s[], int length)
{
if(s[length] == 'a') // last three chars aba, the last 'a' could be part of the suffix
return secondState(s, length+1);
else if(s[length] == 'b') // full suffix matched
return forthState(s, length+1);
else // end of string in not-accepting state
return 0;
}

int forthState(char s[], int length)
{
if(s[length] == 'a') // another char, start a new candidate for the suffix
return secondState(s, length+1);
else if(s[length] == 'b') // another char, can't be part of the suffix, start over
return firstState(s, length+1);
else // end of string in accepting state, yay!
return 1;
// return s[length] == '\0';
// if characters other than 'a' and 'b' need not signal the end of the string
}

关于c - 我的 C 代码中的错误 "segmentation fault",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13575851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com