如何在C ++中打印函数指针的值?

时间:2018-06-14 15:32:10

标签: c++ function pointers

我在这里有一个问题,不明白为什么我不能打印我的函数指针的内存地址。有谁能解释为什么?谢谢!

#include <stdio.h>
#include <iostream>

void displayFourIncrementVar(int &x)
{
    printf( "%d\n", 4 );
    x++;
}

int main()
{
    int y = 4;
    int *z = &y;
    std::cout << *z << std::endl;
    std::cout << z << std::endl << std::endl;
    //printing out z we have an address.  

    void (*garbage)(int&) = &displayFourIncrementVar;
    std::cout << garbage << std::endl << std::endl;
    //Printing out garbage we have no address, instead we get 1, why's that?
    //I thought pointers store memory addresses in dexadecimal.

    int x = 2;
    (*garbage)(x);
    std::cout << x << std::endl; 
    return 0;
}

0 个答案:

没有答案