• <legend id='JcdWe'><style id='JcdWe'><dir id='JcdWe'><q id='JcdWe'></q></dir></style></legend>

        <i id='JcdWe'><tr id='JcdWe'><dt id='JcdWe'><q id='JcdWe'><span id='JcdWe'><b id='JcdWe'><form id='JcdWe'><ins id='JcdWe'></ins><ul id='JcdWe'></ul><sub id='JcdWe'></sub></form><legend id='JcdWe'></legend><bdo id='JcdWe'><pre id='JcdWe'><center id='JcdWe'></center></pre></bdo></b><th id='JcdWe'></th></span></q></dt></tr></i><div id='JcdWe'><tfoot id='JcdWe'></tfoot><dl id='JcdWe'><fieldset id='JcdWe'></fieldset></dl></div>

      1. <small id='JcdWe'></small><noframes id='JcdWe'>

          <bdo id='JcdWe'></bdo><ul id='JcdWe'></ul>
        <tfoot id='JcdWe'></tfoot>

        typedef 如何处理函数指针

        时间:2023-08-26

          <tbody id='a8xzS'></tbody>

            <tfoot id='a8xzS'></tfoot>
              <bdo id='a8xzS'></bdo><ul id='a8xzS'></ul>
            • <i id='a8xzS'><tr id='a8xzS'><dt id='a8xzS'><q id='a8xzS'><span id='a8xzS'><b id='a8xzS'><form id='a8xzS'><ins id='a8xzS'></ins><ul id='a8xzS'></ul><sub id='a8xzS'></sub></form><legend id='a8xzS'></legend><bdo id='a8xzS'><pre id='a8xzS'><center id='a8xzS'></center></pre></bdo></b><th id='a8xzS'></th></span></q></dt></tr></i><div id='a8xzS'><tfoot id='a8xzS'></tfoot><dl id='a8xzS'><fieldset id='a8xzS'></fieldset></dl></div>
              1. <legend id='a8xzS'><style id='a8xzS'><dir id='a8xzS'><q id='a8xzS'></q></dir></style></legend>

                <small id='a8xzS'></small><noframes id='a8xzS'>

                • 本文介绍了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 如何处理函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:与参数组合的各种符号(*、&amp;等)有什么区别? 下一篇:如何在 C++ 中获取文件的 MD5 哈希值?

                  相关文章

                    <i id='Ptxg7'><tr id='Ptxg7'><dt id='Ptxg7'><q id='Ptxg7'><span id='Ptxg7'><b id='Ptxg7'><form id='Ptxg7'><ins id='Ptxg7'></ins><ul id='Ptxg7'></ul><sub id='Ptxg7'></sub></form><legend id='Ptxg7'></legend><bdo id='Ptxg7'><pre id='Ptxg7'><center id='Ptxg7'></center></pre></bdo></b><th id='Ptxg7'></th></span></q></dt></tr></i><div id='Ptxg7'><tfoot id='Ptxg7'></tfoot><dl id='Ptxg7'><fieldset id='Ptxg7'></fieldset></dl></div>
                  1. <tfoot id='Ptxg7'></tfoot>
                  2. <legend id='Ptxg7'><style id='Ptxg7'><dir id='Ptxg7'><q id='Ptxg7'></q></dir></style></legend>

                  3. <small id='Ptxg7'></small><noframes id='Ptxg7'>

                      <bdo id='Ptxg7'></bdo><ul id='Ptxg7'></ul>