• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

Pointer to a function

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
mikechen
Asked:
mikechen
1 Solution
 
jhanceCommented:
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
 
mikechenAuthor Commented:
A lot of books shows (*myfunc)(100). I guess there must be a reason. Any idea ?

0
 
mikechenAuthor Commented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
jkrCommented:
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
 
nietodCommented:
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
 
chensuCommented:
In old C language, "myfunc(100);" can't be used, you must use "(*myfunc)(100);".
0
 
nietodCommented:
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
 
mikechenAuthor Commented:
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
 
nietodCommented:
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
 
mikechenAuthor Commented:
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
 
mikechenAuthor Commented:
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
 
nietodCommented:
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
 
mikechenAuthor Commented:
Thank you.
Mike

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now