0

I'm not clear on what the values that are being returning from calling:

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

They all seem to give me the value 1. What does it mean?

Also, how would I declare

int (*return_f())(char)

to receive a parameter without using 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

}
awesoon
  • 32,469
  • 11
  • 74
  • 99
user1229895
  • 2,259
  • 8
  • 24
  • 26
  • 1
    Your first `std::cout << "fp=" << fp << std::endl;` is undefined behaviour because you didn't initialize the pointer. That you get `0` is accidental. Now if you had initialized it to `0` (defined it as `int (*fp)(int) = 0;` or assigned `0` before the first use), then you'd be guaranteed to get `0`. – celtschk Jul 07 '13 at 08:50
  • 2
    It is a good habit to write code that is more readable. C++ allows for interfaces. You can achieve the same results that are both readable and maintainable. So why make life complicated? – Ed Heal Jul 07 '13 at 09:28

2 Answers2

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

should be

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

The cout operator doesn't have an overload for a function pointer, so it uses bool instead. That's why you always get 1 as output. When I compile your code, I even get a warning for that, telling me that it will always evaluate to true. You should switch on all warnings and try to get rid of them.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Devolus
  • 21,661
  • 13
  • 66
  • 113
4

There is some pointer here who seems not clear :

// 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

Here fp is undefined. Those lines have an undefined behaviour.

After that :

// 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

There are two things here :

  • On the line I pointed, I'm not sure it is what you wanted to test.
  • Also, cout doesn't have an overload to take a function pointer, it will take a bool instead. So it should be :

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

I would suggest you to read this article about function pointer, it explains almost all you need to know : http://www.learncpp.com/cpp-tutorial/78-function-pointers/

Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
  • Thank you for pointing (no pun intended) all that out, and for the article. Also, I thought '&return_func_ptr'would display the address of the function. – user1229895 Jul 07 '13 at 20:09
  • the pointed out line should've been written '&fp_ptr' do I have to reinterpret that as well? ? – user1229895 Jul 07 '13 at 20:19
  • 1
    No you should not. `void&` has no meaning... A reference to a `void` is not correct. And when you want `&fn_ptr`, you want the the address of `fn_ptr`. It is not the same. – Pierre Fourgeaud Jul 07 '13 at 20:27