Solved

Pointer to a function

Posted on 1999-01-28
13
283 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.

914 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now