Taking the address of a function from within this function

Firstly, the question is: is there a reason why you cannot take the address of a function from this function?

#include <stdio.h>

struct foo {
  void(*xf)(void);
  int r;
} sFoo;

void func(void) {
  sFoo.xf = func; /* <-- like this */
}

int main()
{
  func();
  printf("FuncPtr  is: %p\n", sFoo.xf);
  printf("FuncAddr is: %p\n", &func);
  getchar();
  return 0;
}

I cannot think of the reason why this should not work portable, but that does not mean that it does not exist. I tested it with MinGW and MSVC on Windows, and gcc on Ubuntu, and it works great. Standard C is almost silent, except that (C99 5.1.1.2) function references are resolved in the final translation phase.

, , , . , , , , , , .

+4
2

, ?

.

C , , (C99 5.1.1.2) .

; . .

, , , .

, , , . , . , , , , (.. ).

, , , , , .

, , C. , . , , . , , , .

+8

& :

, [], *, l, , .

(, -, lvalue 6.3.2.1p1), , .

, , , , 6.3.2.1p4 :

- , . sizeof, _Alignof , 65) , '' , type '' '.

+2

Source: https://habr.com/ru/post/1690391/


All Articles