qsort - Warning when trying to run Anagram(John Bentley-Programming Pearls)-C -


completely new c. trying hang of linux , c programming getting john bentley's anagram (column 2 believe)program run. pretty sure ive copied code verbatim(had add headers, etc) im receiving warning, when compiled , run squash.c program gives undesired output. ill admit, dont know how charcomp function behaves, or does. (some enlightenment there nice).

#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; } 

here's warning.

sign.c:13:41: warning: incompatible pointer types passing 'int (char *, char *)'       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 parameter '__compar'       here                    __compar_fn_t __compar) __nonnull ((1, 4));                                  ^ 

the qsort() function takes comparison function fourth argument, following signature:

int (*compar)(const void *, const void *) 

therefore, avoid compiler warnings, have modify charcomp() function in following way, fit signature:

int charcomp(const void *x, const void *y) { return *(char *)x - *(char *)y; } 

your charcomp function takes 2 char* pointers , compares first first characters.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -