0

我不清楚调用返回的值是什么:

&next, fp, *fp, &return_func_ptr, fp_ptr, &fp_ptr, *fp_ptr

他们似乎都给了我价值1。这是什么意思?

另外,我将如何声明

int (*return_f())(char)

在不使用 typedef 的情况下接收参数?

#include <iostream>

int next(int n){
    return n+99;    
}

// returns pointer to a function
typedef int (*fptr)(int);               // using typdef
fptr return_func_ptr(){
    return next;    
}

int f(char){
    return 0;
}
int (*return_f())(char){                // how do you pass a parameter here?

    // std::cout << "do something with " << param << std::endl;
    return f;
}



int main()
{

    int x = 5;

    // p points to x
    int *p = &x;
    std::cout << "x=" << x << std::endl;        // 5,               value of x
    std::cout << "&x=" << &x << std::endl;      // 0x7fff6447a82c,  address of x
    std::cout << "p=" << p << std::endl;        // 0x7fff6447a82c,  value of p is address of x
    std::cout << "*p=" << *p << std::endl;      // 5,               value of x (p dereferenced)
    std::cout << "&p=" << &p << std::endl;      // 0x7fff6447a820,  address of p pointer

    // change value of x thru p
    // p = 6;                                   // error,           can't set int* to int
    *p = 6;                                     
    std::cout << "x=" << x << std::endl;        // 6


    int y = 2;
    // int *q = y;                              // error can't initiate with type int, needs int*


    // pointer to a function
    int (*fp)(int);
    std::cout << "&fp=" << &fp << std::endl;        // 0x7fff66da6810,  address of pointer fp
    std::cout << "fp=" << fp << std::endl;          // 0,               value of pointer fp

    fp = &next;                                     // fp points to function next(int)
    fp = next;                                      
    std::cout << "&next=" << &next << std::endl;    // 1,               address of function?
    std::cout << "fp=" << fp << std::endl;          // 1,               value is address of function?
    std::cout << "&fp=" << &fp << std::endl;        // 0x7fff66da6810,  address of pointer fp?
    std::cout << "*fp=" << *fp << std::endl;        // 1,               address of function?

    // calling function thru pointer
    int i = 0;
    i = (*fp)(i);
    std::cout << "i=" << i << std::endl;            // 99
    i = fp(i);
    std::cout << "i=" << i << std::endl;            // 198



    // function returns pointer to function
    fptr fp_ptr = return_func_ptr();
    std::cout << "&return_func_ptr=" << &return_func_ptr << std::endl;      // 1
    std::cout << "fp_ptr=" << *fp_ptr << std::endl;                         // 1
    std::cout << "&fp_ptr=" << *fp_ptr << std::endl;                        // 1
    std::cout << "*fp_ptr=" << *fp_ptr << std::endl;                        // 1

    int j = fp_ptr(1);                              
    std::cout << "j=" << j << std::endl;                                    // 100

}
4

2 回答 2

3
std::cout << "fp_ptr=" << *fp_ptr << std::endl;

应该

std::cout << "fp_ptr=" << (void*)fp_ptr << std::endl;

cout运算符没有函数指针的重载,因此它使用它bool。这就是为什么你总是得到 1 作为输出。当我编译你的代码时,我什至会收到一个警告,告诉我它总是会评估为真。您应该打开所有警告并尝试摆脱它们。

于 2013-07-07T08:20:28.483 回答
3

这里有一些指针似乎不清楚:

// pointer to a function
int (*fp)(int);
std::cout << "&fp=" << &fp << std::endl;        // 0x7fff66da6810,  address of pointer fp
std::cout << "fp=" << fp << std::endl;          // 0,               value of pointer fp

这里fp是未定义的。这些行具有未定义的行为。

之后 :

// function returns pointer to function
fptr fp_ptr = return_func_ptr();
std::cout << "&return_func_ptr=" << &return_func_ptr << std::endl;      // 1
std::cout << "fp_ptr=" << *fp_ptr << std::endl;                         // 1
std::cout << "&fp_ptr=" << *fp_ptr << std::endl;                        // 1
//           ^^^^^^^^^^    ^^^^^^^
std::cout << "*fp_ptr=" << *fp_ptr << std::endl;                        // 1

这里有两件事:

  • 在我指出的那条线上,我不确定这是你想要测试的。
  • 此外,cout没有重载来获取函数指针,bool而是取而代之。所以应该是:

    std::cout << "fn_ptr=" << reinterpret_cast<void*>( fn_ptr ) << std::endl;
    

我建议你阅读这篇关于函数指针的文章,它几乎解释了你需要知道的所有内容:http: //www.learncpp.com/cpp-tutorial/78-function-pointers/

于 2013-07-07T09:21:29.150 回答