Explanation of the code
#include <iostream>
#include <cstdlib>
#include <stdio.h>
using namespace std;
void a()
{
printf("Hello World!\n");
}
int main()
{
typedef int (*fun)(); defining a variable that would point to a function whose return type would be int and wont take any argument.
fun tmp = (fun)a; printf("0x%x\t0x%x\n",tmp,&a); if(tmp()) {
printf("a is ok!\n");
}
else
{
printf("a is not ok!\n");
}
system("pause");
}
I am not sure if any of this will go through you. Another advice learn carefully. You have made a mistake which is considered as dangerous side of c/c++.
change your function a type from void to int
explanation of how return work: (Some of my statement might be wrong. Let me know if I am)
When a function return a value compiler save the return value in AX register first then make the next move.
i.e.
int a()
{
return 10; }
int main()
{
int b=a(); }
now, printf function return the number of character it printed. the value is automatically stored in ax register. In your void function that you have developped will not return anything but ax register will have value 13. so, your if statement will be true but not false.
Another example of function return behave:
#include <stdio.h>
using namespace std;
int a()
{
if(0)
return 1; printf("Hello World!\n");
}
int main()
{
printf("%d\n",a()); }
the answer is the printf function in the main function would print 13. because printf function in in a() function prints 13 character. so it return 13. which remain in AX register. which is printed by printf function in the main function
Why?