gpt4 book ai didi

c - C 中函数指针返回某种类型的指针

转载 作者:行者123 更新时间:2023-11-30 20:24:24 26 4
gpt4 key购买 nike

我在解决下面的代码时遇到问题。

当我编译包含下面这段代码的整个文件时,我看到一条警告(如下所示),如果从整个 *.c 文件中排除,我看不到该警告。

代码:

int * ( * get_ptr1)(int) = (int* )0x234456;
printf("The address of the func_ptr is %x\n", get_ptr1);

警告消息:

Initialization from incompatible pointer type

有人可以解释一下为什么我会看到这个警告以及上面代码中需要纠正的内容吗?我正在使用 C99 标准编译器。

如果我的标题与我的要求不太具体,请耐心等待。

最佳答案

int *int *(*)(int) 是不同的类型。你可以这样写:

int *(*get_ptr1)(int) = (int *(*)(int))0x234456;

printf 的函数指针没有格式说明符。您使用 %x 会导致未定义的行为。 See this thread 提供一些想法。

关于c - C 中函数指针返回某种类型的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33618884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com