简体   繁体   English

三元和强制转换为qsort参数

[英]Ternary and cast as qsort argument

I'm trying to practice pointer to function but I don't understand the technique used. 我正在尝试练习函数指针,但我不了解所使用的技术。

Context: 语境:

char *lineptr[5000];
int strcmp (char *, char *);
int numcmp (char *, char *);`

The function prototype is: 函数原型为:

void qsort ( void *lineptr[], int left, int right, int (*comp)(void *, void *));

I don't understand how the following call works: 我不了解以下通话的工作方式:

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

Specifically this part: 特别是这部分:

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

The function prototype has only 4 argument whereas in the function call, there are 5 parameters and the 5th one is an equation. 函数原型只有4个参数,而函数调用中有5个参数,第5个是方程式。 And this compiles without error. 并且此编译没有错误。

At one point I assumed that it would assign either numcomp or strcmp to the function pointer (*comp) based the value of numeric . 在某一点上,我假设它将根据numeric的值将numcompstrcmp分配给函数指针(*comp) But what I don't understand is how will this assignment happens. 但是我不明白的是,这项任务将如何发生。

Only 4 arguments are passed, the 4th argument 仅传递了四个参数,第四个参数

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

will either pass a pointer to the numcmp (if numeric is truthy) or a pointer to the strcmp function (if numeric is falsy). 将传递一个指向numcmp的指针(如果numeric为真)或一个指向strcmp函数的指针(如果numeric为真)。

(int (*)(void*, void*)) is just a cast to the function signature to help the compiler, because the compiler is expecting (int (*)(void*, void*)) as function pointer (see qsort function prototype) and you're passing in a pointer to numcmp/strcmp which is (int (*)(char*, char*)) . (int (*)(void*, void*))只是对函数签名的强制转换,以帮助编译器,因为编译器期望将(int (*)(void*, void*))作为函数指针(请参阅qsort函数原型),并且您传递了一个指向numcmp/strcmp的指针,该指针是(int (*)(char*, char*))

There are only 4 arguments in: 只有四个参数:

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

Broken down to one-per-line, it looks like: 分解为一行,它看起来像:

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

The last one seems to be the one that's confusing you. 最后一个似乎使您感到困惑。 The ?: expression is selecting one of two functions with the same declared type: ?:表达式正在选择具有相同声明类型的两个函数之一:

numeric?numcmp:strcmp

This is then parenthesized (for precedence) and cast to a function pointer type that takes two void * arguments: 然后将其括起来(优先),并转换为带有两个void *参数的函数指针类型:

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

The type in the cast operator is a pointer to a function that takes two void * arguments and returns an int result. 强制转换运算符中的类型是指向一个函数的指针,该函数带有两个void *参数并返回一个int结果。

The last argument to qsort is a bit complex. qsort的最后一个参数有点复杂。

qsort((void**)lineptr,0,nlines-1,(int(*)(void*,void*))(numeric?numcmp:strcmp));
//                               |<--        one argument                 -->

At the cost of more verbose code, you can simplify that line to 以更多冗长的代码为代价,您可以将该行简化为

int(*compare_function)(void*,void*) = (int(*)(void*,void*))numcmp;
if ( !numeric )
{
   compare_function = (int(*)(void*,void*))strcmp;
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM