-3

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 ?

  • 4
    It doesn't do anything useful, and accomlishes absolutely nothing, whatsoever. Wherever you found that example, it does not seem to be a very good resource for learning C++. – Sam Varshavchik Jun 17 '20 at 16:26
  • Please specify if this question is asking for C or C++. They are different language. – François Andrieux Jun 17 '20 at 16:27
  • 2
    Your title doesn't really reflect the actual question you ask. – Some programmer dude Jun 17 '20 at 16:28
  • `void main()` is not valid C++ https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c – bolov Jun 17 '20 at 16:30
  • 1
    A function pointer to `main` isn't all that useful in C++. The C++ Standard states [The function `main` shall not be used within a program](http://eel.is/c++draft/basic.start.main#3). My language lawyering is weak, so I'm not sure if that extends to holding a pointer to `main`, but having a pointer to `main` suggests that sooner or later someone is going to use that pointer to `main`, and after that [Crom only knows what's going to happen](https://en.cppreference.com/w/cpp/language/ub). – user4581301 Jun 17 '20 at 17:13

3 Answers3

3

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
3

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

int(*)() is a type. It is pointer to a function that returns int and takes no arguments.

(T)expr This is an explicit conversion i.e. a cast. The result of the right hand expression is converted to the type in parentheses. (int(*)())expr converts the right hand expression to a pointer to function.

In (int(*)())p the converted expression is p. p is a variable of type pointer to int. Since the original pointer did not point to a function, the resulting value is arbitrary as far as the C++ standard is concerned.

(int(*)())p==main compares the converted pointer to the address of the main function. The result of the comparison is discarded.


P.S. You've declared that main returns void. This is not allowed in C++. Furthermore, as a consequence, the pointer to function main does not match with the pointer type int(*)(). To fix this, declare main to return int.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

== is a compare operator. You're comparing the pointer to the main() function with the pointer to the variable a. But you're not using the comparison, for example in an if statement, so that line doesn't really do anything.

Brecht Sanders
  • 6,215
  • 1
  • 16
  • 40