Perhaps pointer function or casting is a problem. Not sure

What happens in this code? I do not get this code. It seems like it is doing some sort of listing or using function pointers, but I'm not sure. Understand if someone can help me. Thank.

const char string[]="Hello!";

int main()   
{

    (*(void (*)()) string)(); //Obviously, my problem is this line :)

    return 0;
}
+3
source share
2 answers

First, use cdecl to explain inner gibberish:

$ cdecl
cdecl> explain (void (*)())
cast unknown_name into pointer to function returning void

So (void (*)()) stringinserts stringinto the function pointer. The function pointer is then dereferenced to call the base function. Line is equivalent

void (*fp)() = (*(void (*)()) string)();
(*fp)();

( ) "Hello!" . , . , - . , .

, , , cdecl C .

+4

void (*)() - . (void (*)()) string string . (* ...)() .

, , "Hello!", - .

+3

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


All Articles