Thread: Pointers to functions, K&R, warning: ISO C forbids conversion of object pointer...

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    54

    Pointers to functions, K&R, warning: ISO C forbids conversion of object pointer...

    Hi!

    I am posting the whole program from K&R below, but before
    that please take a look at the line that causes a compiler gcc to
    produce these warnings, both related to the same line of code
    in main function:

    warning: pointer type mismatch in conditional expression
    warning: ISO C forbids conversion of object pointer to function pointer type

    Code:
    quicksort((void **) lineptr, 0, nlines - 1,
                      (int (*) (void *, void *)) (numeric ? numcmp : strcmp));
    What does it mean and why does this warning occur?

    Code:
    /* Program to sort a set of text lines into alphabetic order */
    #include <stdio.h> 
    #include <string.h>
    #include <stdlib.h>
    
    #define MAXLINES 5000 /* max #lines to be sorted */
    
    char *lineptr[MAXLINES]; /* pointers to text lines */
    
    int readlines(char *lineptr[], int nlines);
    void writelines(char *lineptr[], int nlines);
    void quicksort(void *v[], int left, int right,
                   int (*comp) (void *, void *));
    int numcmp(char *, char *);
    
    int main(int argc, char *argv[])
    {
        int nlines; /* number of input lines read */
        int numeric = 0;
        
        if (argc > 1 && strcmp(argv[1], "-n") == 0)
        {
            numeric = 1;
        }
        if ((nlines = readlines(lineptr, MAXLINES)) >= 0)
        {
            printf("\n");
            quicksort((void **) lineptr, 0, nlines - 1,
                      (int (*) (void *, void *)) (numeric ? numcmp : strcmp));
     /* the above call to quicksort causes gcc to produce warnings */
       
            writelines(lineptr, nlines);
            printf("%i\n", nlines);
            return 0;
        }
        else
        {
            printf("error: input too big to sort\n");
            return 1;
        }
    }
    
    #define MAXLEN 1000 /* max length of any input line */
    int get_line (char *, int);
    char *alloc(int);
    
    /* readlines: read input lines */
    int readlines(char *lineptr[], int maxlines)
    {
        int len, nlines;
        char *p, line[MAXLEN];
        nlines = 0;
        
        while ((len = get_line(line, MAXLEN)) > 0)
        {
            if (nlines >= maxlines || (p = alloc(len)) == NULL)
            {
                return -1;
            }
            else
            {
                line[len - 1] = '\0'; /* delete newline */
                strcpy(p, line);
                lineptr[nlines++] = p;
            }
        }
        return nlines;
    }
    
    /* writelines: write output lines */
    void writelines(char *lineptr[], int nlines)
    {
        int i;
        
        for (i = 0; i < nlines; i++)
        {
            printf("%s\n", lineptr[i]);
        }
    }
    
    /* getline: read a line into s, return length */
    
    int get_line (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;
    }
    
    #define ALLOCSIZE 10000 /* size of a varibale space */
    static char allocbuf[ALLOCSIZE]; /* storage for alloc */
    static char *allocp = allocbuf;
    
    char *alloc(int n) /* return pointer to n characters */
    {
        if (allocbuf + ALLOCSIZE - allocp >= n) /* it fits */
        {
            allocp += n;
            return allocp - n; /* previous value of p */
        }
        else   /* not enough room */
            return 0;
    }
    
    void afree(char *p) /* free storage pointed to by p */
    {
        if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
            allocp = p;
    }
    
    /*numcmp: compare s1 and s2 numerically */
    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;
    }
    
    /* sort v[left] .... v[right] into increasing order */
    void quicksort(void *v[], int left, int right,
               int (*comp)(void *, void *))
    {
        int i, last;
        void swap(void *v[], int i, int j);
        
        if (left >= right) /* do nothing if array contains */
            return;        /* fewer than two elements; left and right are indeces */
        
        swap(v, left, (left + right)/2); /* move partition elem to v[O]*/
        
        last = left;
        
        for (i = left+1; i <= right; i++) /* partition */
            if ((*comp)(v[i], v[left]) < 0)
                swap(v, ++last, i);
        
        swap(v, left, last); /* restore partition elem */
        quicksort(v, left, last-1, comp);
        quicksort(v, last+1, right, comp);
    }
    
    void swap (void *v[], int i, int j)
    {
        void *temp;
        
        temp = v[i];
        v[i] = v[j];
        v[j] = temp;
    }
    Thank you!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducol
    What does it mean and why does this warning occur?
    It means that you tried to convert a pointer to an object to be a pointer to a function. It does not make sense in this case, so the problem could be something else.

    This:
    Code:
    int numcmp(char *, char *);
    is not compatible in type with strcmp. You should change it to:
    Code:
    int numcmp(const char *, const char *);
    and of course update the function definition.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    54
    Quote Originally Posted by laserlight View Post
    It means that you tried to convert a pointer to an object to be a pointer to a function. It does not make sense in this case, so the problem could be something else.

    This:
    Code:
    int numcmp(char *, char *);
    is not compatible in type with strcmp. You should change it to:
    Code:
    int numcmp(const char *, const char *);
    and of course update the function definition.
    Thank you very much! I have made corrections, and now the program compiles without warnings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-14-2012, 01:44 AM
  2. Replies: 3
    Last Post: 06-18-2012, 01:06 PM
  3. ISO c++ forbids comparisson btween pointers and integers
    By Cursius in forum C++ Programming
    Replies: 4
    Last Post: 01-10-2010, 03:57 PM
  4. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  5. Replies: 2
    Last Post: 01-29-2003, 03:32 PM

Tags for this Thread