我的c代码中的"分段错误"错误

Bas*_*adr 3 c segmentation-fault

#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;
}
Run Code Online (Sandbox Code Playgroud)

它给了我一个分段错误或核心倾倒我很困惑!谁能解释为什么它给了我这种虫子???? 并告诉如何调试,使我的代码运行得非常清楚!!

我真的很累这个:(

对不起,我的英语不好

Dan*_*her 7

你有一个无限的递归,

return (secondState(s, length++));
Run Code Online (Sandbox Code Playgroud)

length传递的参数是length增量之前的值,所以你只看第一个char.

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

另一个注意事项,

gets(string);
Run Code Online (Sandbox Code Playgroud)

非常不安全,如果输入超过9个字符,则在分配的内存之外写入.使用

fgets(string, sizeof string, stdin);
Run Code Online (Sandbox Code Playgroud)

代替.


好吧,因为它只需要上面提到的修复和一个返回值的更改,所以逻辑的大部分是正确的,固定代码:

// #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
}
Run Code Online (Sandbox Code Playgroud)