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 ?
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 ?
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.
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
.
==
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.