Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Pointer to a function

Posted on 1999-01-28
13
Medium Priority
?
314 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 400 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

810 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