Question about function pointer in C

Here are the following announcements:

void qsort(void *lineptr[], int left, int right, int (*comp)(void *, void *));
int numcmp(char *, char *);
int strcmp(char *s, char *t);

Then, somewhere in the program there is the following call:

  qsort((void**) lineptr, 0, nlines-1, 
                    (int (*)(void*,void*))(numeric ? numcmp : strcmp));

(Ignore the first three arguments and numeric).

I ask what it is:

(int (*)(void*,void*))(numeric ? numcmp : strcmp)

I understand that I’m qsortexpecting a “function pointer” that receives two pointers voidand returns int“as the 4th argument, but how does what is written above satisfy this? It seems to me that this is some kind of cast, because it consists of two parentheses, but that would be very strange. Because it takes a function and makes this function “a pointer to a function that gets two pointers voidand returns int.” That doesn't make sense.
(I followed this rule that the type typein parentheses before the variable pushes the variable to this type).

So, I think I'm just wrong, maybe someone can tell me how to read this, what order?

+5
9

, , . , numcmp . C. , ,

(int (*)(int*,int*))

qsort, void. , . , , - , .

(int (*)(void*,void*))(numcmp )
+4

-

(numeric ? numcmp : strcmp)

, qsort. , numcmp. , strcmp. :

int (*comparison_function)(void*,void*) = 
    (int (*)(void*,void*))(numeric ? numcmp : strcmp);
qsort((void**) lineptr, 0, nlines-1, comparison_function);
+4

. . , , , .

+4

, qsort() const:

void qsort(void *base, size_t nmemb, size_t size,
           int (*compar)(const void *, const void *));

, "char **", "char *".

, :

#include <stdlib.h>    /* qsort() */
#include <string.h>    /* strcmp() */

int num_cmp(const void *v1, const void *v2)
{
    int i1 = *(const int *)v1;
    int i2 = *(const int *)v2;
    if (i1 < i2)
        return -1;
    else if (i1 > i2)
        return +1;
    else
        return 0;
}

int str_cmp(const void *v1, const void *v2)
{
    const char *s1 = *(const char **)v1;
    const char *s2 = *(const char **)v2;
    return(strcmp(s1, s2));
}

. .

, qsort(). , , .

, , , :

result = (*pointer_to_function)(arg1, arg2, ...);

:

result = pointer_to_function(arg1, arg2, ...);

, .

+3

,

(int (*)(void*,void*))(numeric ? numcmp : strcmp)

cast

(int (*)(void*,void*))

(numeric ? numcmp : strcmp)

- , . , , , , , , .. . , . , , , (*) , . , . (void*,void*) , . int . . : : / C : .

, , , : cdecl C :

cdecl> explain (int (*)(void*,void*))
cast unknown_name into pointer to function (pointer to void, pointer to void) returning int
cdecl> declare my_var as array 5 of pointer to int
int *my_var[5]
cdecl>

: i?

int *(*(*i)[])(int *)

rot13, cdecl, ( !):

pqrpy> rkcynva vag *(*(*v)[])(vag *)
qrpyner v nf cbvagre gb neenl bs cbvagre gb shapgvba (cbvagre gb vag) ergheavat cbvagre gb vag
pqrpy>
+2

, , . , , , , , "". , , , , .

:

. , , , , .


... , .

asm, , - asm, , .

+2

, , :

typedef int (*PFNCMP)(void *, void *);

PFNCMP comparison_function;

if (numeric)
{
    comparison_function =  numcmp;
}
else
{
    comparison_function = strcmp;
}

qsort((void**) lineptr, 0, nlines-1, comparison_function);

.

+1

, . " , void int", .

0

numcmp strcmp , char* int. qsort , void* int. , . , void* . , : strcmp:

 int strcmp(char *, char *);

strcmp :

 int (strcmp)(char *, char *)

( ), char *. , strcmp:

 int (*)(char *, char *)

Therefore, when you need to include another function that will be compatible with strcmp, you must use the above value as the type to be executed.

Similarly, since the argument of the qsortcomparator takes two, void *and thus the odd cast!

0
source

All Articles