function pointers and pointer to function

what is the difference between function pointer and pointer to a function
are they both same
prasad2315Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
There shouldn't be if the terms have their normal meaning. But usually, "function pointer" is used.
0
Infinity08Commented:
       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;
0
evilrixSenior Software Engineer (Avast)Commented:
Interesting Q, I'm not 100% there is an official answer (someone will no doubt prove me wrong). If I was considering the semantics of both I would say...

Function Pointer: A type that can be instatiated to be a pointer to a function

e.g.  typedef void (*foo_fp_t)();

Where foo_fp_t is a type of function pointer to type void (*)();

Similar to int * is an int pointer

Pointer to a function: An instance of a Function Pointer that can point to a function

void foo();

foo_fp_t fp = foo;

Where fp is a pointer to a function, in this case function foo()

Similar to int * p, where p in an instance of int* and is a pointer to int

I restate, this is how I personally would interpret the semantic difference. I based this on the differnce between template class and class template. A template class is a class of template whereas a class template is an instance of a template class.

-Rx.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Infinity08Commented:
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/
0
evilrixSenior Software Engineer (Avast)Commented:
>> 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
0
evilrixSenior Software Engineer (Avast)Commented:
>> 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 :)

0
Infinity08Commented:
>> 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.
0
evilrixSenior Software Engineer (Avast)Commented:
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 :)
0
Infinity08Commented:
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.
0
evilrixSenior Software Engineer (Avast)Commented:
>> Semantics lol.
Sorry, what do you mean? :)
0
evilrixSenior Software Engineer (Avast)Commented:
Oooh... I just got a new rank... ta v much :)
0
Infinity08Commented:
>> >> Semantics lol.
>> Sorry, what do you mean? :)

Was that a serious question ?

        http://en.wikipedia.org/wiki/Semantics
0
evilrixSenior Software Engineer (Avast)Commented:
>> Was that a serious question ?
Um, no. It was meant to be a pun (hence the smiley). :-)
0
Infinity08Commented:
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.
0
evilrixSenior Software Engineer (Avast)Commented:
>> All replies in this post should be taken into account, as they all answer the question.
Agreed.
0
Infinity08Commented:
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.
0
evilrixSenior Software Engineer (Avast)Commented:
To be fair, I think I made it clear in my post it was my interpretation, no?
0
Infinity08Commented:
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 ;)
0
evilrixSenior Software Engineer (Avast)Commented:
>> 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 :)
0
prasad2315Author Commented:
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
0
evilrixSenior Software Engineer (Avast)Commented:
@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.
0
Infinity08Commented:
>> 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 ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.