Call function without parameter and parenthesis

后端 未结 3 1028
盖世英雄少女心
盖世英雄少女心 2020-12-11 05:25

In the following code snippet, the main function calls foo function without any parameter and parenthesis. It is strange that this code can be compiled by gcc. I actually ch

相关标签:
3条回答
  • 2020-12-11 05:58

    This is no different than having any other type of expression and ignoring its value, like:

    int main(void)
    {
      42;
      return 0;
    }
    

    there's nothing special, this is not calling the function since the function-call operators () are not being used. All you're doing is "computing" the functions' address, then ignoring it.

    0 讨论(0)
  • 2020-12-11 06:00

    foo is not getting called, it is just referred (and not assigned to anything).

    0 讨论(0)
  • 2020-12-11 06:14

    The expression foo is evaluated (giving the address of the function), and the result is discarded. Without the () operator, the function isn't called.

    0 讨论(0)
提交回复
热议问题