小编mus*_*bur的帖子

Weird qsort behavior, what's wrong?

Why doesn't the code below sort the month names? Base address is correct, number of elements is correct, size of each element is correct, cmp function is correct. gcc -W -Wall -ansi -pedantic doesn't complain a peep.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int by_name(const void *a, const void *b) {
    fprintf(stderr, "%p %p\n", a, b);
    return strcmp((const char *)a, (const char *)b);
}
int main(void) {
    char *months[] = {
        "January",
        "Februar",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September", …
Run Code Online (Sandbox Code Playgroud)

c sorting c-strings qsort string-literals

1
推荐指数
1
解决办法
68
查看次数

标签 统计

c ×1

c-strings ×1

qsort ×1

sorting ×1

string-literals ×1