Link to home
Start Free TrialLog in
Avatar of prasad2315
prasad2315

asked on

function pointers and pointer to function

what is the difference between function pointer and pointer to a function
are they both same
Avatar of Infinity08
Infinity08
Flag of Belgium image

There shouldn't be if the terms have their normal meaning. But usually, "function pointer" is used.
       typedef int (*FunPtr)(char *);

        int fun1(char *str) {
            /* do something */
        }

        int fun2(char *str) {
            /* do something else */
        }

        FunPtr my_fun_ptr = &fun1;

my_fun_ptr is a function pointer that points to the fun1 function. You can also make it point to the fun2 function like this :

        my_fun_ptr = &fun2;
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You could make the distinction evilrix makes, but both terms are commonly used interchangeably with preference for "function pointer".

More info on function pointers :

        http://www.function-pointer.org/
>> A template class is a class of template whereas a class template is an instance of a template class.
Sorry, I realized I transposed those two definitions (sorry), I'll restate...
A template class is a class of template whereas a template class is an instance of a template class.

Anyway, more info on function pointers: http://en.wikipedia.org/wiki/Function_pointer
>> A template class is a class of template whereas a template class is an instance of a template class.
Still wrong! -- what is wrong with me today? I'll try one last time (promise)

The term "class template" refers to a kind of template. The term "template class means a class instantiated from a class template.

That, I promise, is the correct definition. I wish I hadn't started this analogy now :)

>> both terms are commonly used interchangeably

Or more specifically, it depends on the context you use it in. If the fact that it's a pointer is more important than the fact that it's pointing to a function, then "pointer to function" is used to stress the "pointer" part. Otherwise "function pointer" is used.
I think the point I8 (and myself badly) are trying to make is it largely depends upon the context in which the phrase is being used. Maybe I should have just said that at the start and been done with it :)
You'll see for example that on the site I posted, they use both terms interchangeably.

Another reason to use "pointer to function" is if something is specified about that function :

        a pointer to a function which takes a char* as argument and returns and int

but then really the "which" part refers to "function" and not to "pointer to a function".

Oh well ... Semantics lol.
>> Semantics lol.
Sorry, what do you mean? :)
Oooh... I just got a new rank... ta v much :)
>> >> Semantics lol.
>> Sorry, what do you mean? :)

Was that a serious question ?

        http://en.wikipedia.org/wiki/Semantics
>> Was that a serious question ?
Um, no. It was meant to be a pun (hence the smiley). :-)
The accepted answer is not absolute - it is just a personal interpretation. It's not because it's an instance of a function pointer that you should call it a "pointer to function". You can very well call it a function pointer, which is exactly what a lot of people do.

Both terms are very similar as I said, and their use depends on context. What I'm trying to say is that the accepted answer is not the only thing you should consider. All replies in this post should be taken into account, as they all answer the question.
>> All replies in this post should be taken into account, as they all answer the question.
Agreed.
prasad2315, do you understand what I said in my last post ? Since you accepted only that specific reply, it seems like you're taking that as the one and only answer. And I want to make sure that you know that's not the case, because otherwise you will get very confused when reading stuff about function pointers where people use the terms inconsistently with evilrix's interpretation.

That doesn't mean that his interpretation is wrong ... It just means that you should see it as just that : an interpretation.
To be fair, I think I made it clear in my post it was my interpretation, no?
I'm not contesting that, evilrix. I just want to make sure prasad2315 understands that ... to avoid confusion in the future.

Semantics is a difficult topic, especially in a language that is not your own. You can easily consider interpretations as truths, and get very confused because of that. I know from experience ;)
>> I'm not contesting that, evilrix
It's ok, I'm just making sure I hadn't mis-lead the OP (at least not intentionally) is all :)
Avatar of prasad2315
prasad2315

ASKER

actually before posting this question what i understood was both function pointer anf pointer to a  function are identical to each other and we use ther terms interchangeably
this is little bit confusing topic
answer have strengthened my previous understanding of this particular topic
i have accepted a single answer because i felt i can differentitate between the two for my understanding
@prasad2315

You need to understand my interpretation is subjective... mine... and doesn't reflect an authorative answer. I did state that in my original post.

The definition; however, about class template and template class isn't subjective; they are real definitions (when I eventually managed to get it right). I was losely basing my interpretation of the difference between function pointer and pointer to function on the difference between class template and template class; however, this is a purely subjective opinion.
>> i have accepted a single answer because i felt i can differentitate between the two for my understanding

The answer(s) you accept has/have to be the one(s) that helped you. You shouldn't just pick one ;)

Us experts generally don't care that much about the points, but we do care about the recognition of the time and effort we put into answering questions. It's a small task to select the answers that best helped you, and it shows that you appreciate what we do ;)