Thread: conflicting data types

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    31

    conflicting data types

    this won't compile

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define ALLOCSIZE 10000
    #define MAXLINES 5000
    #define MAXLEN 1000
    
    char *lineptr[MAXLINES];
    static char allocbuf[ALLOCSIZE];
    static char *allocp = allocbuf;
    
    void swap(void *v[], int i, int j);
    int getline(char line[], int maxline);
    char *alloc(int);
    int readlines(char *lineptr[], int nlines);
    void writelines(char *lineptr[], int nlines);
    
    void qsort(void *lineptr[], int left, int right, int (*comp)(void *, void *));
    
    
    int numcmp(char *s, char *t);
    
    int strcmp(char *s, char *t){
        for ( ; *s == *t; s++, t++)
            if (*s == '\0')
                return 0;
        return *s - *t;
    }
    
    
    int main(int argc, char *argv[]){
    
        int nlines;
        int numeric = 0;
    
        if (argc>1 && strcmp(argv[1], "-n") == 0)
            numeric = 1;
    
        if( (nlines = readlines(lineptr, MAXLINES)) >= 0 ){
            qsort((void **)lineptr, 0, nlines-1, (int (*)(void *, void *))(numeric ? numcmp : strcmp));
            writelines(lineptr, nlines);
            return 0;
        } else {
            printf("input too big to sort\n");
            return -1;
        }
    }
    
    char *alloc(int n){
        if (allocbuf + ALLOCSIZE - allocp >= n){
            allocp += n;
            return allocp - n;
        } else {
            return 0;
        }
    }
    
    int getline(char s[],int lim)
    {
        int c, i;
        for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
            s[i] = c;
        if (c == '\n') {
            s[i] = c;
            ++i;
        }
        s[i] = '\0';
        return i;
    }
    void swap(void *v[], int i, int j){
        void *temp;
        temp = v[i];
        v[i] = v[j];
        v[j] = temp;
     }
    int numcmp(char *s1, char *s2){
        double v1, v2;
    
        v1 = atof(s1);
        v2 = atof(s2);
    
        if (v1 < v2)
            return -1;
        else if(v1 > v2)
            return 1;
        else
            return 0;
    }
    
    void qsort(void *v[], int left, int right, int (*comp)(void *, void *)){
        int i, last;
    
        void swap(void *v[], int, int);
        if( left >= right )
            return;
        swap(v, left, (left+right)/2);
        last = left;
        for(i = left; i <= right; i++)
            if((*comp)(v[i], v[left]) < 0)
                swap(v, ++last, i);
        swap(v, left, last);
        qsort(v, left, last-1, comp);
        qsort(v, last+1, right, comp);
    }
    
    
    int readlines(char *lineptr[], int maxlines){
        int len, nlines;
        char *p, line[MAXLINES];
    
        nlines = 0;
        while( (len = getline(line, MAXLEN)) > 0 )
            if( nlines >= maxlines || (p = alloc(len)) == NULL )
                return -1;
            else {
                line[len-1] = '\0';
                strcpy(p, line);
                lineptr[nlines++] = p;
            }
        return nlines;
    }
    
    void writelines(char *lineptr[], int nlines){
        int i;
        for(i = 0; i < nlines; i++){
            printf("%s\n", lineptr[i]);
        }
    }
    it gives error thus

    ||=== Build: Debug in dbg (compiler: GNU GCC Compiler) ===|
    C:\Users\goget\Documents\strcmp.c|20|error: conflicting types for 'qsort'|
    C:\Program Files (x86)\CodeBlocks\MinGW\include\stdlib.h|371|note: previous declaration of 'qsort' was here|
    C:\Users\goget\Documents\strcmp.c|25|error: conflicting types for 'strcmp'|
    C:\Program Files (x86)\CodeBlocks\MinGW\include\string.h|43|note: previous declaration of 'strcmp' was here|
    C:\Users\xx\Documents\strcmp.c||In function 'main':|
    C:\Users\xx\Documents\strcmp.c|42|warning: passing argument 1 of 'qsort' from incompatible pointer type|
    C:\Users\xx\Documents\strcmp.c|20|note: expected 'void **' but argument is of type 'char **'|
    C:\Users\xx\Documents\strcmp.c|92|error: conflicting types for 'qsort'|
    C:\Program Files (x86)\CodeBlocks\MinGW\include\stdlib.h|371|note: previous declaration of 'qsort' was here|
    ||=== Build failed: 3 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
    i understand that in the stdlib.h file, the declaration for qsort is

    Code:
    _CRTIMP void __cdecl qsort(void*, size_t, size_t,
                   int (*)(const void*, const void*));
    which means the first argument is a void pointer

    but the one use in the book k&r is


    Code:
    void qsort(void *lineptr[], int left, int right, int (*comp)(void *, void *));
    which is an array of void pointers and this is needed for the program. what can i do to make it compile?

    2. Also, for strcmp function in string.h, the declaration is

    Code:
    _CRTIMP int __cdecl __MINGW_NOTHROW    strcmp (const char*, const char*)  __MINGW_ATTRIB_PURE;
    but in the program in k&r its
    Code:
    int strcmp(char *s, char *t)
    so why is the compiler saying incompatible type??

    i'm confused sirs..

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You need to name your own functions myStrcmp() and myQsort() to avoid any conflict with the standard library functions of the same name.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conflicting Types
    By TKEsquad in forum C Programming
    Replies: 5
    Last Post: 05-08-2015, 08:31 AM
  2. Replies: 7
    Last Post: 01-22-2014, 09:48 AM
  3. conflicting types
    By Sotiris Kaniras in forum C Programming
    Replies: 27
    Last Post: 01-01-2013, 09:18 AM
  4. Conflicting types???
    By kwikness in forum C Programming
    Replies: 11
    Last Post: 10-07-2007, 11:53 PM
  5. conflicting types for...
    By dudinka in forum C Programming
    Replies: 3
    Last Post: 05-14-2005, 07:03 AM

Tags for this Thread