Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

c语言EXC_BAD_ACCESS (code=2, address=0x102a3bf74)

我的代码如下

#include <stdio.h>
#include <string.h>
void permutation(char str[], int len, int cur)
{
    if (cur == len - 1) {
        printf("%s
", str);
    }

    char tmp;
    for (int i = cur; i < len; i++) {
        tmp      = str[cur];
        str[cur] = str[i];
        str[i]   = tmp;

        permutation(str, len, cur++);

        tmp = str[cur];
        str[cur] = str[i];
        str[i]   = tmp;
    }
}
int main() {
    char* str = "abc";
    int len = strlen(str);
    permutation(str, len, 0);
}

str[cur] = str[i]这里报EXC_BAD_ACCESS ,但是我又不知道问题出在哪。还请各位大佬帮忙看看。
image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

两个问题

char* str = "abc";  -> char str[] = {'a','b','c',0};
cur++  -> cur+1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...