开发者

pointer to function assignment

开发者 https://www.devze.com 2023-04-01 08:01 出处:网络
Why the assignment without \'&\' is c开发者_开发百科ompiling in the following code? I compiled the code with GCC 3.4.6. Is it right to assign without &, or this is a \"feature\" of GCC?

Why the assignment without '&' is c开发者_开发百科ompiling in the following code? I compiled the code with GCC 3.4.6. Is it right to assign without &, or this is a "feature" of GCC?

void func() {
}

int main() {
  typedef void (*F)();

  F f;
  f = &func; // the way of assigning pointer to function.
  f = func;  // this is also working.

  (*f)();

  return 0;
}


This is perfectly ok. Both statements are totally equivalent. The function lvalue is converted to a pointer to function through a standard conversion. §4.3/1:

An lvalue of function type T can be converted to an rvalue of type “pointer to T.” The result is a pointer to the function.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号