Solved

Function Pointer

Posted on 2003-11-18
10
504 Views
Last Modified: 2010-04-02
Dear helper,

  Suppose I have the following code:

int function1(int);

int main(void) {
  int (*fPtr)(int) = function1;
  return 0;
}

  Why can I call the function using pointer to function in two ways?

 - (*fPtr)(5);
 - fPtr(5);

  I have read a book that says both of the statements are the same. I am not so clear why they are the same? Please explain me.

Thank you in advance,

Dina
0
Comment
Question by:SDN_KH
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
10 Comments
 

Expert Comment

by:FatNewbie
ID: 9777020
So what is a pointer? A pointer is a way to get at another object. Essentially it is a way to grab an instance of an object and then either pass that instance a message or retreive some data from that object. A pointer is actually just an address of where an instance is held in memory.

Some piece of your program can either possess an instance of an object, or know where an instance of an object is. An instance of an object is a chunk of memory that is big enough to store all the member data of that object. A pointer is an address that explains how to get to where the instance is actually held in memory.
0
 
LVL 15

Assisted Solution

by:efn
efn earned 25 total points
ID: 9777134
There isn't any logical reason.  It's just a decision the language designers made.  The first form is consistent with the rest of the language; the second form was probably just added for convenience, since there isn't much you can do with a function pointer other than call the function it addresses.

--efn
0
 

Assisted Solution

by:amarzee
amarzee earned 25 total points
ID: 9778046
Dear Dina,
I also read the same thing in one book they are saying that since it is function pointer.

First they allowed syntax (fPtr)(5);


People started commenting that fptr is pointer it should be de-referenced then they given another syntax
(*fptr)(5);


bye
amar
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Assisted Solution

by:mrwad99
mrwad99 earned 25 total points
ID: 9779011
When using a pointer to the method of a class, you *need* to dereference it.

Class Test
{
  int F1(){ return 1;}
  int F2() { return 2;}
};

int main()
{
   int (Test::*pFunction)() = 0; // Initialise pointer to NULL

   Test* ptr = new Test();

   pFunction = Test::F1;

   /* Note if the below line was ptr->pFunction, the compiler would complain stating that             pFunction is not a member of the Test class, which it is quite right about !
   */
   
   cout << (ptr->*pFunction)() << endl;
   return 0;
}

This code should compile; have not got one on me (on me ?!?).  Basically if you do not dereference pFunction the compiler wont be too happy.  But at least it wont be a linker or runtime error :)

HTH
0
 
LVL 2

Accepted Solution

by:
MirkoLMV earned 25 total points
ID: 9801069

Dear Dina,

The name of a C/C++ function is not a simple identifier, but a constant pointer which stores the starting address of the executable code of the function (first machine code instruction). When type checking are performed by compilers, there is not considered a generic type of pointer to the function, but a particular pointer to a function which returns a certain type and has a certain list of arguments (of certain types, placed in a defined order).
 int (*fPtr)(int)  is a pointer to a function which returns an integer and requires a single parameter, which is also an integer. A definitely different data type would be  void (*pFun)(char*, int, bool)  which would be a function which does not return any value and requires three parameters: first is a pointer to a character (and can be used for an array of character, actually a string in C/C++), the second an integer, and the third is a boolean.
The pointer to a function is used when the actual processing needed on a certain moment is known just when the program is running (on run-time) rather than on compile-time (depending on user input, contents of a data file, previous computations, etc). For the fPtr as defined in your code, there can be employed more functions like
 int funAdd (int a) { return (3 + a) ; }
 int funMultiply (int a) { return (5 * a) ; }
 int funFormula (int a) { return (a * a + a + 4) ; }
The assignment of the pointer to such a function is a simple statement like
 fPtr = funAdd ;
 fPtr = funMultiply ;
 fPtr = funFormula ;
 (and all these functions should be employed at least once).
It is a system of indirect identification of the functions, and the most conventional call statement would be :
 fPtr(iOurData) ;  The value stored by fPtr is the entry point of the specified function's code, which is to be executed after receiving the value of the parameter.
The (*fPtr) (iOurData) ; is inconvenient, but has the same effect of calling a function. One could expect a retrieval of the value of the pointed-to variable (since a dereferencing is suggested), which is in this case the microprocessor operation code of the first instruction. However, compilers are build to start the execution of the encountered machine code rather than delivering such an machine operation code for storage in variables.
You should attempt a small demonstrative application to practice the indirect identification of functions, relying on my hints or something similar. Such demonstrative applications are the best method to confirm the assimilation of a new programming technique.

Good luck !
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10249305
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: amarzee {http:#9778046} & mrwad99 {http:#9779011} & MirkoLMV {http:#9801069}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
 
LVL 15

Expert Comment

by:efn
ID: 10249379
amarzee's comment was much the same as mine.  mrwad99's comment was true, but not relevant to the question, which did not mention pointers to member functions.  MirkoLMV's comment is mostly correct and probably helpful.  I suggest splitting points among me, amarzee, and MirkoLMV.

--efn
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 10251137
>> but not relevant to the question

Yeah fair comment efn; totally agree.

Split as suggested by efn :)
0
 
LVL 2

Expert Comment

by:MirkoLMV
ID: 10337699
I am glad that I was of a help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question