发送一个转换为指向接受指针的函数的指针的参数如何工作?我不应该发送参数的地址吗?

How does sending an argument that's casted to a pointer to a function that accepts pointer work? Shouldn't I send the ADDRESS of the argument?

我理解 print1 函数的工作方式,但是为什么调用 print2 函数时我不需要传递参数的 地址 但我只需要将它转换为 void 指针?我不明白输出是如何正确的。我说 (void*)20 是什么意思?然后 print2 函数中的整个 "int n = (int)param 是做什么的?

#include <stdio.h>
#include <stdlib.h>

void print1(void* param)
{
    int n = *((int*)param);
    printf("You sent: %d\n", n);
}

void print2(void* param)
{
    int n = (int)param;
    printf("You sent: %d\n", n);
}

int main()
{
    int number = 10;
    print1(&number);

    print2((void*)20);

    return 0;
}

(void *)2020 转换为指针。根据 C 2018 6.3.2.3 5,结果是实现定义的。

print2((void*)20); 将该指针发送到 print2

print2 中,int n = (int)param; 将该指针转换回 int。根据 C 2018 6.3.2.3 6,结果是实现定义的。一个常见的结果是恢复原始整数,前提是它没有 运行 与 C 实现中使用的寻址机制的限制或复杂性冲突。

printf("You sent: %d\n", n); 将此恢复的 int 发送到 printf