typedef 如何处理函数指针 [英] How typedef works for function pointers

查看:32
本文介绍了typedef 如何处理函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我可能患有可怕的意外程序员"病,至少在涉及到 typedef 和函数指针时.所以我一直在试验涉及这些的各种组合,以根据我得到的所有输出来分析结果.

I think I may be suffering from the dreaded "accidental programmer" disease, at least when it comes to typedefs and function pointers. So I've been experimenting with all kinds of combinations involving these to analyse the results based on all the output I get.

但是当我不断尝试不同的组合时,我没有分析结果,现在只是迷失在过程中.

But as I kept on trying different combinations, instead of analyzing the results I'm now just lost in process.

我希望你们能帮我解决这个烂摊子.

I'm hoping you guys will help me figure out this mess.

第一个代码示例

typedef void (print)(void);
void do_something (void) { printf("Hello World
"); }

print *pr;
pr = &do_something;
pr(); // Hello World

第二个代码示例

typedef void (print)(void);
void do_something (void) { printf("Hello World
"); }

print *pr;
pr = do_something;
pr(); // Hello World

上面两个代码示例是如何工作的,就好像'&'对函数名没有影响

How do both the above code examples work, it's as if '&' has no effect on function name

第三个代码示例

typedef void (print)(void);
void do_something (void) { printf("Hello World
"); }

print pr;
pr = do_something; // compile error
pr = &do_something; // compile error
pr();

我希望上述任务之一能在这里工作,但该死!我真的不明白函数指针(也许还有 typedef).

I was hoping one of the above assignments to work here but damn! I really don't understand function pointers (and maybe typedef too).

推荐答案

函数名的地址和普通函数名的地址是一样的,所以 & 对函数没有影响名字.

The address of a function name and the plain function name both mean the same thing, so & has no effect on a function name.

同样,当使用函数指针时,多次解引用不是问题:

Similarly, when using function pointers, multiple dereferencing isn't a problem:

#include <stdio.h>
typedef void print(void);
static void dosomething(void) { printf("Hello World
"); }

int main(void)
{
    print *f1 = dosomething;
    print *f2 = &dosomething;
    f2();
    (f1)();
    (*f1)();
    (**f2)();
    (***f1)();
    (****f2)();
    (*****f1)();
}

在以下情况下可以干净地编译:

That compiles cleanly under:

gcc -O3 -g -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes 
    -Wold-style-definition -std=c99 xx.c -o xx

我不会说多颗星是好的风格;不是.这是奇怪的,而且(是的,你可能会这么说)有悖常理".一颗就足够了(一颗星主要是给像我这样在标准说可以通过指针调用函数而不使用 (*pointer_to_function)(arg1, arg2) 表示法;如果你愿意,你可以只写 pointer_to_function(arg1, arg2)").是的,这很奇怪.不,谢天谢地,没有其他类型(或类型的类别)表现出相同的行为.

I would not claim that multiple stars is good style; it isn't. It is 'odd, and (yes, you may say it) perverse'. One is sufficient (and the one star is mainly for people like me who learned to program in C before the standard said "it is OK to call a function via a pointer without using the (*pointer_to_function)(arg1, arg2) notation; you can just write pointer_to_function(arg1, arg2) if you like"). Yes, it is weird. No, no other type (or class of types) exhibits the same behaviour, thank goodness.

这篇关于typedef 如何处理函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆