开发者

segmentation fault [closed]

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜