Solved

Pointer to a function

Posted on 1999-01-28
13
310 Views
Last Modified: 2010-04-16
The following is a piece of code:

typedef      int (*PFUNC)(int);

int      func1(int nValue)
{
      printf("func1() is called with %d.\n", nValue);
      return(nValue);
}

main()
{
      PFUNC      myfunc;

      myfunc = func1;
        myfunc(100);
        (*myfunc)(100);
        (**myfunc)(100);
        (***myfunc)(100);
}

I saw somebody uses myfunc(100), and somebody use (*myfunc)(100). Then I try some other way like what I showed above. They all give me the right answer.

So what is the right syntax? And why it happens in this way.

Thanks,


Mike
0
Comment
Question by:mikechen
[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
13 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 1185413
It is my opinion that only " myfunc(100);" is correct.  The fact that the others are giving you the correct result is just a result of the particular compiler you are using.  I.e., you're lucky.
0
 

Author Comment

by:mikechen
ID: 1185414
A lot of books shows (*myfunc)(100). I guess there must be a reason. Any idea ?

0
 

Author Comment

by:mikechen
ID: 1185415
Hi, jhance,

I just take a look on the on-line help in VC++6.0. They have an example like this:

<<

#include <iostream.h>
#include <stdlib.h>

extern void func1();         // Declare 4 functions.
extern void func2();         // These functions are assumed to be
extern void func3();         //  defined elsewhere.
extern void func4();
                             // Declare synonym for pointer to
typedef void (*PVFN)();      //  function that takes no arguments
                             //  and returns type void.

void main( int argc, char * argv[] )
{
    // Declare an array of pointers to functions.
    PVFN pvfn1[] = { func1, func2, func3, func4 };

    // Invoke the function specified on the command line.
    if( argc > 0 && *argv[1] > '0' && *argv[1] <= '4' )
   (*pvfn1[atoi( argv[1] ) - 1])();
}

>>

I agree with your opnion, I myself think myfunc(100) is the right syntax. But I really want to know the reason behind this. So I want to open the question again to see whether there is some other input.

Thank you very much.

Mike


0
Technology Partners: 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 86

Expert Comment

by:jkr
ID: 1185416
Well, it is quite ambivalent - i personally use '(pFunc)();'
Most compilers accept both for plain 'C' functions - but insist on '*' when using pointer to C++ member functions. IMHO, '(*pFunc)()' is the syntactically more correct way, as e.g.
char c1, c2 = 'a';
char* pc = &c2;
c1 = *pc;
illustrates the usage for the '*' operator on a variable of type 'char', and it should be used this way for function pointers, also...
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1185417
This is a "feature" of the language, not a bug.

This works very similar to the fact that the name of an array varialbe also functions like a pointer to the type in the array.  In this case, the name of a function also functions are a pointer to the function's type.  Thus you can do

typedef int (*PFUNC)(int);

int func1(int nValue) ;

PFUNC Ptr1 = &func1;
PFUNC Ptr2 = func1

Note that you don't have to use the & operetor to get the address of the funcion, just the name alone is enough.  Now thats for generating pointers, things get weird when it comes to using the pointers.

continues
0
 
LVL 23

Expert Comment

by:chensu
ID: 1185418
In old C language, "myfunc(100);" can't be used, you must use "(*myfunc)(100);".
0
 
LVL 22

Expert Comment

by:nietod
ID: 1185419
When the () operator is used on a function pointer, it does not bind with the function pointer.  That is, it does not operate on the function pointer.  Instead the * operator is called (automatically) to dereference the pointer.  and the () operator acts on the result.  Thus you can specify the * operator if you want but don't have to so you can do

typedef int (*PFUNC)(int);

   int func1(int nValue) ;

   PFUNC Ptr = func1
   (*Ptr)(5);
  Ptr(5);

That explains two cases, what about  the ** or *** case?  Those are legal too and work fine.
Well what is happening is that whey you dereference the function pointer  with the first * you get the function.  But the function automatically acts like a function pointer.  (as I showed above).  So that pointer then gets dereferenced automatically.   This can happen again and again so there is no limit to the number of *'s you can use.

Make sense.

0
 

Author Comment

by:mikechen
ID: 1185420
Thank you for your answer. Does this apply to both C and C++ ? Or it is compiler dependent. I have only VC++6.0 at this moment so hard to test it.

Thanks,

Mike

0
 
LVL 22

Expert Comment

by:nietod
ID: 1185421
It is not compiler dependant.  I thought it applies to C as well (In fact I thought it was a hold-over of some of C's weird syntax rules), but chensu suggested that is not true.  
0
 

Author Comment

by:mikechen
ID: 1185422
Hi, Nietod,

Where do you get this from ?
<<
When the () operator is used on a function pointer, it does not bind with the function pointer.  That is, it does not operate on the function pointer.  Instead the * operator is called (automatically) to dereference the pointer.  and the () operator acts on the result.  Thus you can specify the * operator if you want but don't have to so you can do
>>

Thank you.

Mike

0
 

Author Comment

by:mikechen
ID: 1185423
Hi, Nietod,

Where do you get this from ?
<<
When the () operator is used on a function pointer, it does not bind with the function pointer.  That is, it does not operate on the function pointer.  Instead the * operator is called (automatically) to dereference the pointer.  and the () operator acts on the result.  Thus you can specify the * operator if you want but don't have to so you can do
>>

Thank you.

Mike

0
 
LVL 22

Expert Comment

by:nietod
ID: 1185424
Two sources, but the one I still have was incomplete.   The one I have is
C++ how to program" by Dietel and Dietel. it disscusses the fact  the * and & operators are unnecessary, but does not mention the fact that * can be used repeatedly.  The description of that came--I beleive--from one of Allan Clark's C++ tip mailings.  

This is not covered in Stroustrup's "C++ Programming Llanguage".  But this is similar to what happens with the binding of the -> operator, which is covered in there.
0
 

Author Comment

by:mikechen
ID: 1185425
Thank you.
Mike

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 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