Rick Huenink
Rick Huenink

Reputation: 21

What does it mean to have a function pointer type?

I have a class assignment that says:

Write a declaration for a function that takes two int parameters and returns an int, and declare a vector whose elements have this function pointer type.

Since the function and vector are both int is this correct? I'm really fuzzy on pointers still. This is what I have:

#include <iostream>
#include <vector>
using std::cin; using std::cout; using std::endl; using std::vector;

// This is my function that take two ints and returns and int
int AddFunc ( int a, int b) { int addResult; addResult = a + b; return (addResult);}

int main()
{
    vector <int> v1; // Declare a vector whose elements have this functions pointer types
    int add1, add2, add3 = 0;
        cout << "Enter two numbers to be added with my AddFunc function: ";
        cin >> add1 >> add2;
        add3 = AddFunc (add1, add2);
        cout << "The numbers added equal: " << add3 << endl;
        v1.push_back(add3);
        cout << "The first element in the vector v1 is: " << v1 [0] << endl;
    return 0;
}

Upvotes: 1

Views: 620

Answers (3)

Nikos C.
Nikos C.

Reputation: 51920

The assignment wants a vector of function pointers for your function. Function pointers are a bit difficult to read if you're not used to them. Here's a method you can use to write a function pointer type.

First, write the argument list of the function. AddFunc() takes two int arguments, so at first you have:

(int, int)

For this to be a function pointer, you prefix it with (*):

(*)(int, int)

Lastly, you prefix the whole thing with the return type of the function. In this case, int:

int (*)(int, int)

You can use that as-is as the type of your vector:

std::vector<int (*)(int, int)> v;

This will work. However, it's usually more clear to actually define your own type for the function pointer (using typedef). At first, it seems tricky to do so, but it's easy. You already wrote down the type using the above method. The only thing that's missing is to give it a name. You do that by writing the name after the * in (*). For example (*func_ptr), so you have:

int (*func_ptr)(int, int)

And that's all you need for a typedef:

typedef int (*func_ptr)(int, int);

Now whenever you need that particular function pointer type, instead of:

int (*)(int, int)

you can write:

func_ptr

instead. So your vector declaration becomes:

std::vector<func_ptr> v;

As far as vector is concerned, func_ptr is a pointer type just like any other.

Function pointers differ from "regular" pointers in that "dereferencing" them means calling the function they point to. You don't actually dereference a function pointer. Instead, you use the () operator on them. Doing that calls the function they point to.

Assigning to a function pointer means assigning it the address of a function. For example, if you have a variable of type func_ptr (which you typedefed above):

func_ptr AddFunc_p;

You can assign it the address of AddFunc() with:

AddFunc_p = AddFunc;

Note the missing parentheses in AddFunc. You only write the name of the function, since you don't want to actually call that function, but rather want its address so you can assign it to AddFunc_p. Now you can call AddFunc() through AddFunc_p with:

AddFunc_p(some_int, another_int);

You should now be able to deduct how to put function pointers in a vector and how to apply the () operator on its elements. It will help to keep in mind that the vector simply contains elements of type func_ptr.

Upvotes: 1

In C++ you have the abilty to define a function pointer, like so:

int (*pfunc) (int, int);

This is a variable, to which you can assign the address of a function, as long as it has the specified signature, like so:

pfunc = AddFunc; // Assign the adress of a function
pfunc(1,2) // Now call the function through the pointer

Now, note that pfunc is the variable name, and its "official" type is int (*) (int,int) Naturally, this can all get very confusing, so you'd probably wanna typedef it:

typedef int (*AdderFunc)(int, int);
AdderFunc pfunc = AddFunc;
pfunc(1,2);

Upvotes: 2

Kerrek SB
Kerrek SB

Reputation: 477620

The function pointer type is int (*)(int, int). You want this:

typedef int (*fptr)(int, int);

std::vector<fptr> v;

Example:

int add(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }

v.push_back(&add);
v.push_back(&mul);

And then something like this:

for (f : v) { std::cout << "f(5, 7) = " << f(5, 7) << std::endl; }

Upvotes: 4

Related Questions