Добрый день! Моя система: Mac OS X 10.7.5 gcc: 4.2.1 Ниже я добавлю всю программу из K&R, но перед этим хотела бы показать warnings, которые выдает compiler.
warning: pointer type mismatch in conditional expression warning: ISO C forbids conversion of object pointer to function pointer type
Оба предупреждения выданы в отношении одной строки:
quicksort((void **) lineptr, 0, nlines - 1,
(int (*) (void *, void *)) (numeric ? numcmp : strcmp));
Что означают эти предупреждения и почему они возникают и как исправить?
Вся программа:
/* 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;
}
Спасибо!