简体   繁体   English

char数组上的stdlib.h qsort()在C中崩溃

[英]stdlib.h qsort() on char array crashes in c

#include <stdio.h>
#include <stdlib.h>

int cmpfunc(const void *a, const void *b) {
   const char *ia = (const char*)a;
   const char *ib = (const char*)b;
   return *ia - *ib;
}

int is_permutation(char *s1, char *s2){
    int i, n1, n2;


    n1 = sizeof(s1)/sizeof(s1[0]);
    n2 = sizeof(s2)/sizeof(s2[0]);


    if(n1 != n2){
        return 0;
    }


    qsort(s1, n1, sizeof(s1), cmpfunc);
    qsort(s2, n2, sizeof(s2), cmpfunc);

    for (i = 0; i < n1;  i++)
       if (s1[i] != s2[i])
         return 0;

    return 1;
}

int main(){
    char s1[5] = "check";
    char s2[5] = "check";

    printf("%d", is_permutation(s1,s2));

    return 0;
}

It just crashes with no compiler errors. 它只是崩溃而没有编译器错误。 I've checked and the qsort crashes the program, everything else seems to work appropriately. 我检查了一下,qsort使程序崩溃了,其他所有东西似乎都正常工作。 Any help? 有什么帮助吗?

I compile with "gcc -g -ansi -pedantic -Wall prog.c -o prog" 我用“ gcc -g -ansi -pedantic -Wall prog.c -o prog”进行编译

sizeof(s1) &c. sizeof(s1) &c。 is not a function of the number of elements in the array. 不是数组中元素数量的函数。 This is because s1 has decayed to a pointer type. 这是因为s1衰减为指针类型。

strlen can be used to get the length of a string, but you'd need to write strlen可用于获取字符串的长度,但是您需要编写

char s1[6] = "check";

or better still, 还是更好

char s1[] = "check";

to allow space for the NUL-terminator. 为NUL终止子留出空间。

I've checked and the qsort crashes the program, everything else seems to work appropriately. 我检查了一下,qsort使程序崩溃了,其他所有东西似乎都正常工作。 ? No, have a run in debugger ? 不,可以在调试器中运行吗? Try compiling with -g option and run gdb and do bt . 尝试使用-g选项进行编译,然后运行gdb并执行bt

The problematic statement are 有问题的陈述是

n1 = sizeof(s1)/sizeof(s1[0]); /* it will results in 4/1 that is 4 */
n2 = sizeof(s2)/sizeof(s2[0]);

Instead rotate loop inside s1 and find the length or use strlen() to find the length of s1 and s2 as 而是在s1内部旋转循环并找到长度,或使用strlen()来找到s1s2的长度,如下所示:

for(n1 = 0;s1[n1]!='\0'; n1++); /* dummy loop, at the end of loop, n1 will be length of s1 */
for(n2 = 0;s2[n2]!='\0'; n2++);

And

qsort(s1, n1, sizeof(s1[0]), cmpfunc);
qsort(s2, n2, sizeof(s2[0]), cmpfunc);

Here is the sample is_permutation() function 这是示例is_permutation()函数

int is_permutation(char *s1, char *s2){
        int i, n1, n2;
        for(n1 = 0;s1[n1]!='\0'; n1++); /* dummy loop, at the end of loop, n1 will be length of s1 */
        for(n2 = 0;s2[n2]!='\0'; n2++);

        if(n1 != n2){
                return 0;
        }
        qsort(s1, n1, sizeof(s1[0]), cmpfunc);
        qsort(s2, n2, sizeof(s2[0]), cmpfunc);

        for (i = 0; i < n1;  i++)
                if (s1[i] != s2[i])
                        return 0;

        return 1;
}

Most importantly char s1[5]="check" doesn't have space for \\0 char. 最重要的是char s1[5]="check"没有用于\\0 char的空间。 So either make char s1[6] or char s1[]= "check" 因此,可以使char s1[6]char s1[]= "check"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM