【发布时间】:2016-03-17 14:44:15
【问题描述】:
我在 c 中有这段代码
const char * array[] = {
"1",
"2",
"helloworld",
"worldhello",
"3",
"zzzzzzzzzz",
"Zzzzzzzzzz",
"zzzzzzzzzZ",
};
/* n_array is the number of elements in the array. */
#define n_array sizeof(array)/sizeof(const char *)
/* Compare the strings. */
static int compare (const void * a, const void * b)
{
/* The pointers point to offsets into "array", so we need to
dereference them to get at the strings. */
return strcmp (*(const char **) a, *(const char **) b);
}
int main ()
{
int i;
qsort (array, n_array, sizeof (const char *), compare);
for (i = 0; i < 50000; i++) {
printf ("%d: %s.\n", i, array[i]);
}
return 0;
}
我希望输出变成这样
helloworld
worldhello
Zzzzzzzzzz
zzzzzzzzzz
zzzzzzzzzZ
我如何修改我的代码以获得输出
【问题讨论】:
-
我喜欢你在 for 循环中的
i < 50000。有什么有意义的输出吗?顺便说一句,你仍然有字符串“1”、“2”和“3”,所以你永远无法得到你想要的输出。 -
只需依次打印每一个,就完成了。或者也许编辑您的问题,使其有意义。