[英]Warning when trying to run Anagram(John Bentley-Programming Pearls)-C
完全是C語言的新手,只是試圖通過運行John Bentley的Anagram(我相信第2列)程序來擺脫linux和C編程的束縛。 可以肯定的是,ive逐字復制了此代碼(必須添加標頭等),但是我收到了警告,當我使用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));
^
qsort()
函數將比較函數作為第四個參數,並帶有以下簽名:
int (*compar)(const void *, const void *)
因此,為了避免編譯器警告,您必須按照以下方式修改charcomp()
函數以適合該簽名:
int charcomp(const void *x, const void *y) { return *(char *)x - *(char *)y; }
您的charcomp
函數只需將兩個char*
指針指向,然后首先比較它們的第一個字符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.