我希望我把它写得简短明了了,我想在下面做些什么。
对于SOF问题来说,代码是相当复杂的,我不认为我可以使它变得更简单,同时让其他人直接测试它。
所以我把相关的部分切下来放在这里。
为什么我会得到这个错误,你能帮我解决吗?
任何帮助都是非常感谢的!
谢谢。
char words[100][WORD_LENGTH];
char temp[WORD_LENGTH];
// scan the next %s from stream and put it to temp
while(fscanf(file, "%s", temp) > 0){
// printf("reducer reads: %s\n", temp);
strcpy(words[arr_i], temp);
printf("%d -- %s\n", arr_i, words[arr_i]);
arr_i++;
}在第二行,我得到分割错误的错误。(可能还会漏油)
int thunk = WORD_LENGTH;
qsort_r(&words, sizeof(words)/sizeof(words[0]), sizeof(words[0]), cmpstringp, &thunk);来自"man qsort":
static int cmpstringp(const void *p1, const void *p2) {
/* The actual arguments to this function are "pointers to
pointers to char", but strcmp(3) arguments are "pointers
to char", hence the following cast plus dereference */
return strcmp(* (char * const *) p1, * (char * const *) p2);
}发布于 2015-03-05 15:07:40
根据man qsort
qsort_r()函数与qsort()相同,只是比较函数compar有第三个参数
您的比较函数是采用两个参数。
更新:真正的崩溃原因如下。您将传递给类型为char[WORD_LENGTH]的数组的比较函数元素,而不是man qsort示例中的char*。因此传递给比较函数的参数是p1 = &words_,它是指向要比较的字符串的指针。在char指针的情况下,它将是(char **),只是指向char/string指针的指针。因此,行strcmp(* (char * const *) p1, * (char * const *) p2);中的强制转换是不必要的,而且是有害的,因为在您的示例中,最左边的取消引用是问题的原因。移除它们,只留下strcmp(p1, p2)。
顺便提一下,这个问题再次强调了字符串数组声明(如char *[]和char [][] )之间的区别。
https://stackoverflow.com/questions/28880786
复制相似问题