关于 C++ 演员问题

Dav*_*gea 3 c++ type-conversion typecast-operator

#include <stdlib.h>

int int_sorter( const void *first_arg, const void *second_arg )
{
    int first = *(int*)first_arg;
    int second = *(int*)second_arg;
    if ( first < second )
    {
        return -1;
    }
    else if ( first == second )
    {
        return 0;
    }
    else
    {
        return 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

在这段代码中,这一行是什么意思?

int first = *(int*)first_arg;
Run Code Online (Sandbox Code Playgroud)

我认为这是排版。但是,从

指向 int 的指针指向指向 int 的指针

这里有点困惑。谢谢

?

Bla*_*ear 5

first_arg 被声明为 void*,因此代码从 void* 转换为 int*,然后它取消引用指针以获取从中指向的值。该代码等于以下代码:

int first = *((int*) first_arg);
Run Code Online (Sandbox Code Playgroud)

并且,如果仍然不清楚:

int *p = (int *) first_arg;
int first = *p;
Run Code Online (Sandbox Code Playgroud)