Anonii
December 2nd, 2007, 06:07 PM
Greetings again,
since the previous thread got too polluted (which is a good thing), I'm making a new thread to give you some new retarded C questions for you to laugh at!
(By the way, you guys totally solved my questions in the previous thread)
Code:
#include <stdio.h>
#include <string.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 qsort(void *lineptr[], int left, int right,
int (*comp)(void *, void *));
int numcmp(char *, char *);
/* sort input lines */
main(int argc, char *argv[])
{
int nlines; /* number of input lines read */
int numeric = 0; /* 1 if numeric sort */
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;
}
}
/* qsort: sort v[left]...v[right] into increasing order */
void qsort(void *v[], int left, int right,
int (*comp)(void *, void *))
{
int i, last;
void swap(void *v[], int, int);
if (left >= right) /* do nothing if array contains */
return; /* fewer than two elements */
swap(v, left, (left + right)/2);
last = left;
for (i = left+1; 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);
}
#include <stdlib.h>
/* 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;
}
Questions ^.^:
1)
In the function prototype:
void qsort(void *lineptr[], int left, int right,
int (*comp)(void *, void *));
we see that the qsort() is waiting for a pointer to a function, and that that function has two void arguments. What troubles me now is, the void *. What does that mean? Also when main() calls qsort(), it also asks for void *. Why doesn't it say void *foo ?
2)
Now, I'll take you down to the main() function, and specifically in line:
qsort((void**) lineptr, 0, nlines-1,
(int (*)(void*,void*))(numeric ? numcmp : strcmp));
(You saw this coming, didn't you?)
Alrighty, what does: (void **) lineptr, do?
3)
I can guess that: (int (*)(void*,void*))(numeric ? numcmp : strcmp) will call either the numcp or the strcmp with 2 void arguments, depending on the value of numeric. But wouldn't this make the code look like:
int (*)(void*,void*)(numcp), if numeric is 1? Is that normal code? Does (*) take the value of numcmp? And what's with void *, void *? What arguments are they? Isn't numcmp waiting for two char arguments? Will it transform the void type to char, before entering numcmp?
</questions>
Thank you for your answers!
since the previous thread got too polluted (which is a good thing), I'm making a new thread to give you some new retarded C questions for you to laugh at!
(By the way, you guys totally solved my questions in the previous thread)
Code:
#include <stdio.h>
#include <string.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 qsort(void *lineptr[], int left, int right,
int (*comp)(void *, void *));
int numcmp(char *, char *);
/* sort input lines */
main(int argc, char *argv[])
{
int nlines; /* number of input lines read */
int numeric = 0; /* 1 if numeric sort */
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;
}
}
/* qsort: sort v[left]...v[right] into increasing order */
void qsort(void *v[], int left, int right,
int (*comp)(void *, void *))
{
int i, last;
void swap(void *v[], int, int);
if (left >= right) /* do nothing if array contains */
return; /* fewer than two elements */
swap(v, left, (left + right)/2);
last = left;
for (i = left+1; 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);
}
#include <stdlib.h>
/* 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;
}
Questions ^.^:
1)
In the function prototype:
void qsort(void *lineptr[], int left, int right,
int (*comp)(void *, void *));
we see that the qsort() is waiting for a pointer to a function, and that that function has two void arguments. What troubles me now is, the void *. What does that mean? Also when main() calls qsort(), it also asks for void *. Why doesn't it say void *foo ?
2)
Now, I'll take you down to the main() function, and specifically in line:
qsort((void**) lineptr, 0, nlines-1,
(int (*)(void*,void*))(numeric ? numcmp : strcmp));
(You saw this coming, didn't you?)
Alrighty, what does: (void **) lineptr, do?
3)
I can guess that: (int (*)(void*,void*))(numeric ? numcmp : strcmp) will call either the numcp or the strcmp with 2 void arguments, depending on the value of numeric. But wouldn't this make the code look like:
int (*)(void*,void*)(numcp), if numeric is 1? Is that normal code? Does (*) take the value of numcmp? And what's with void *, void *? What arguments are they? Isn't numcmp waiting for two char arguments? Will it transform the void type to char, before entering numcmp?
</questions>
Thank you for your answers!