Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this code if i write the printf statement then the address appears to be like 4324231 but if i want to print the address of function using cout statement then it prints the address of function as 1 , why the address is changed if i print it with cout and printf?
C++
int main()
{
    int fun();
    
    //printf("%d",&fun);
    cout<<(&fun);
    getch();
    return 0;
}

int fun()
{
}
Posted
Updated 3-Mar-12 18:06pm
v2

1 solution

The statement "cout<<(&fun);" will treat (&fun) as a boolean value. The address of func is casted to bool (always evaluate as `true' ), then it is output as "1".

C++
#include <iostream>
#include <conio.h>
using namespace std;

int fun();

int main()
{
    int fun();

    printf("%x\n",&fun);
    cout<<(void*)(&fun);
    getch();
    return 0;
}

int fun()
{
    return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900