Explanation of function pointers

0 votes

I'm having trouble comprehending some C++ syntax when paired with function pointers and function declarations, specifically:

When we wish to declare a kind of function, we usually write something like:

I'm happy with typedef void(*functionPtr)(int); 

FunctionPtr is now a type that represents a pointer to a function that returns void and takes an int as an argument.

We may put it to use as follows:

typedef void(*functionPtr)(int);

void function(int a){
    std::cout << a << std::endl;
}

int main() {

    functionPtr fun = function;
    fun(5);

    return 0;
}

And 5 are printed on a screen.

We have a pointer to function fun, which we assign to function - function, and we run this function using a pointer. 

Cool.
Now, according to some books, function and pointer to function are treated similarly, so after declaring the function() function, whenever we say function, we mean both real function and pointer to function of the same type, so the following compiles and every instruction yields the same result (5 printed on a screen):

int main() {

    functionPtr fun = function;
    fun(5);
    (*fun)(5);
    (*function)(5);
    function(5);

    return 0;
}

So, as long as I can conceive that pointers to functions and functions are essentially the same, it's great with me.

Then I thought, since the pointer to function and the real function are the same, why can't I perform the following:

typedef void(functionPtr)(int); //removed *

void function(int a){
    std::cout << a << std::endl;
}

int main() {

    functionPtr fun = function;
    fun(5);

    return 0;
}

This gives me following error:

prog.cpp:12:14: warning: declaration of 'void fun(int)' has 'extern' and is initialized functionPtr fun = function;

Jun 20 in C++ by Nicholas
• 6,240 points
27 views

1 answer to this question.

0 votes

It's a little perplexing.

Function type and pointer to function type are distinct kinds (no more similar than int and pointer to int). 

However, in virtually all cases, a function type decays to a reference to a function type. 

In this context, rotting roughly refers to conversion (there is a difference between type conversion and decaying, but you are probably not interested in it right now).

What matters is that practically every time you use a function type, you end up with a reference to the function type. 

But take note of the nearly - almost every time is not always!

And there are rare circumstances where it does not.

typedef void(functionPtr)(int);
functionPtr fun = function;

This code tries to clone one function to another (not the pointer! the function!) 

However, this is not feasible since functions in C++ cannot be copied. 

The compiler does not let this, and I'm surprised you got it compiled (you say you got linker errors?)

Now for the code:

typedef void(functionPtr)(int);
functionPtr function;
function(5);

function does not shadow anything. Compiler knows it is not a function pointer which can be called, and just calls your original function.

answered Jun 21 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

What is meant with "const" at end of function declaration?

I received a book in which it is written: class Foo { public: int ...READ MORE

Jul 28 in C++ by Nicholas
• 6,240 points
24 views
0 votes
0 answers
0 votes
0 answers

How can I get the size of a C++ function?

In C++, how can I find the size& ...READ MORE

Aug 5 in C++ by Nicholas
• 6,240 points
26 views
0 votes
1 answer

Why would anyone use set instead of unordered_set?

Unordered sets must compensate for their O(1) ...READ MORE

answered Jun 1 in C++ by Damon
• 4,960 points
81 views
0 votes
0 answers

What is the point of function pointers?

The purpose of function pointers is difficult ...READ MORE

Aug 17 in C++ by Nicholas
• 6,240 points
23 views
0 votes
1 answer

What is a smart pointer and when should I use one?

A smart pointer is similar to a ...READ MORE

answered Jun 2 in C++ by Damon
• 4,960 points
30 views
0 votes
1 answer

Using :: (scope resolution operator) in C++

You're mostly correct regarding cout and cin. ...READ MORE

answered Jun 27 in C++ by Damon
• 4,960 points
36 views
0 votes
1 answer

In C++ abs( *a - *b) does not return absolute value of negative number

On the first line, you reallocated *a, and it is now utilising that new value on the second line.  int origa = *a; *a = abs(origa + ...READ MORE

answered Jun 27 in C++ by Damon
• 4,960 points
30 views
0 votes
1 answer

How do I create an array of pointers?

Student** db = new Student*[5]; // To allocate ...READ MORE

answered Aug 5 in C++ by Damon
• 4,960 points
55 views
0 votes
1 answer

Syntax of priority queue

We must first include the queue header file in order to establish a priority queue in C++. #include <queue> Once we import this file, we ...READ MORE

answered May 31 in C++ by Damon
• 4,960 points
33 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP