【发布时间】:2015-12-28 20:26:50
【问题描述】:
对 C 完全陌生。只是试图通过运行 John Bentley 的 Anagram(我相信是第 2 栏)程序来掌握 linux 和 C 编程的窍门。很确定我逐字复制了这段代码(必须添加标题等),但我收到了一个警告,当用我的 squash.c 程序编译和运行时,它会给出不想要的输出。我承认,我什至不知道这个 charcomp 函数的行为方式,或者它的作用。 (那里的一些启示也很好)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int charcomp(char *x, char *y) {return *x - *y;}
#define WORD_MAX 100
int main(void)
{
char word[WORD_MAX], sig[WORD_MAX];
while (scanf("%s", word) != EOF) {
strcpy(sig, word);
qsort(sig, strlen(sig), sizeof(char), charcomp);
printf("%s %s\n", sig, word);
}
return 0;
}
这是警告。
sign.c:13:41: warning: incompatible pointer types passing 'int (char *, char *)'
to parameter of type '__compar_fn_t' (aka 'int (*)(const void *, const
void *)') [-Wincompatible-pointer-types]
qsort(sig, strlen(sig), sizeof(char), charcomp);
^~~~~~~~
/usr/include/stdlib.h:766:20: note: passing argument to parameter '__compar'
here
__compar_fn_t __compar) __nonnull ((1, 4));
^
【问题讨论】:
-
Programming Pearls 作为语言入门书?很奇怪。