简体   繁体   English

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

[英]Function pointer returning a pointer of some type in C

I am having trouble solving this below piece of code.我在解决下面这段代码时遇到了问题。

When i compile the entire file with this below piece of code included, i see a warning ( shown below), which i don't see if excluded from the entire *.c file.当我编译包含下面这段代码的整个文件时,我看到一个警告(如下所示),我看不到它是否从整个 *.c 文件中排除。

CODE:代码:

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

Warning message:警告信息:

Initialization from incompatible pointer type

Could anyone please explain me why i am seeing this warning and what need to be corrected in the above code.任何人都可以解释我为什么看到这个警告以及需要在上面的代码中更正什么。 I am using a C99 standard compiler.我使用的是 C99 标准编译器。

Please bear with me if my title is not very specific to what i have asked.如果我的标题不是我所问的非常具体,请耐心等待。

int * and int *(*)(int) are different types. int *int *(*)(int)是不同的类型。 You could write:你可以写:

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

There is no format specifier for function pointers with printf .带有printf函数指针没有格式说明符。 Your use of %x causes undefined behaviour.您使用%x会导致未定义的行为。 See this thread for some ideas. 有关一些想法, 请参阅此线程

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

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