开发者

segmentation fault [closed]

开发者 https://www.devze.com 2023-02-19 17:44 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.
#include <stdio.h>
#include <string.h>

int test(char ch [10],int i,int j )
{
    if(i>=j) return 1;
    else if  (ch[i]!=ch[j开发者_Python百科]) return 0;
    else return (test(ch,i++,j--)); 
}

int main ()
{
    char  ch[10];
    int m,k;

    printf("Donner une chaine de caracteres :\n");
    scanf("%s",ch);
    k=strlen(ch);
    m=test(ch,0,k-1);
    if (m==1) printf ("expression palindrome \n");
    else printf ("expression non palindrome \n");
    return 0;
}


Try replacing this:

else return (test(ch,i++,j--));

...with this:

else return (test(ch,i+1,j-1));

There's no need to assign back into 'i' and 'j' when making that call, since you don't reference them again in the same function invocation. Moreover, i++ evaluates to the original value of i, and not the value of i + 1 (which is what you want here).

So your original code would never actually modify i and j, which would cause it to recurse infitely and cause a stack overflow (so I can't believe people were saying that this wasn't appropriate for SO).

0

精彩评论

暂无评论...
验证码 换一张
取 消