如果我使用cout和printf进行打印,为什么更改了函数的地址? [英] why the address of function is changed if i print it with cout and printf?

查看:164
本文介绍了如果我使用cout和printf进行打印,为什么更改了函数的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此代码中,如果我编写了printf语句,则该地址似乎像4324231,但是如果我想使用cout语句来打印函数的地址,则它将函数的地址打印为1,为什么如果我将地址更改为用cout和printf打印吗?

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?

int main()
{
    int fun();
    
    //printf("%d",&fun);
    cout<<(&fun);
    getch();
    return 0;
}

int fun()
{
}

推荐答案

语句"cout<<(& fun);"将(&fun)视为布尔值. func的地址被强制转换为bool(始终评估为"true"),然后将其输出为"1".

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".

#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;
}


这篇关于如果我使用cout和printf进行打印,为什么更改了函数的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆