Search code examples
c++cpointerscastingint

Cast a pointer to point to main function in C


Hi i see an example of pointer as below:

void main()
{
    int a=197,*p=&a;
    (int(*)())p==main;
} 

I do not know what the statement (int(*)())p==main do ?


Solution

  • If we split it up into smaller parts we have

    • (int(*)())p
    • ==
    • main

    The first is a cast of p to a pointer taking either an indeterminate number of arguments (in C) or no arguments (in C++).

    It compares this pointers to the pointer that main decays to.

    Then throws away the boolean result (the result is not used).

    Note that there is a very big semantic difference between C and C++ here.