Hi Chris, Bob,

I admit I'm rusty with my C, but in C++ at least, I'm afraid both you and 
Bob are mistaken.

Declaring "Boolean oldTrap (FieldPtr, EventPtr)" does not make oldTrap
a function pointer. Instead, it declares "oldTrap" as a function taking
two arguments and returning Boolean. The (*) syntax must be used to make
a function pointer. Having the '*' does not give you a pointer to a
pointer to a function.

If we had:

   Boolean oldTrap (FieldPtr, EventPtr);
   oldTrap = get_old_trap();                 // Error! oldTrap not lvalue.

If we instead had:

   Boolean (*oldTrap) (FieldPtr, EventPtr);
   oldTrap = get_old_trap()                  // Fine, oldTrap is a fn ptr.

Now oldTrap is a function pointer, not a function. To use the oldTrap, it
is optional to dereference it. That is:

   result = (*oldTrap) (fld, event);         // Fine
   oldTrap (fld, event);                     // Also fine

Perhaps this is why the confusion:

   Boolean realfunction (FieldPtr, EventPtr);
   Boolean (*oldTrap) (FieldPtr, EventPtr);

   oldTrap = realfunction;                   // Legal!

It makes it seem as though 'realfunction' is a pointer to function. It
is not. The following code is exactly equivalent to the last line above:

   oldTrap = &realfunction;                  // This is what is really
                                             // happening.

That is, as a matter of convenience, once you have declared a function
pointer, it is legal to omit the "&" when getting the address, and to 
omit the "*" when using the address.
                                       
I'm sure the rules are the same with C, but you can test all of the
above with your favorite C or C++ compiler. =-)

Take care,

-Ade


At 03:47 PM 5/11/99 -0700, you wrote:
>Bob is basically correct, but here's the root of the problem:
>
>a function pointer is (as should be obvious from the phrase) a pointer.  so
>by declaring "Boolean oldTrap(FieldPtr, EventPtr);" you have already made
>oldTrap be a pointer.  so sticking that extra "*" in the declaration of
>oldTrap is what's hosing you, because it turns "oldTrap" into a pointer to a
>pointer to a function, which is not what you want.
>
>
>-----Original Message-----
>From: Bob Ebert <[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
>Cc: Palm Developers Forum List <[EMAIL PROTECTED]>
>Date: Tuesday, May 11, 1999 10:24 AM
>Subject: Re: Basic Hackmaster Problem
>
>
>>At 6:39 PM -0700 5/10/99, Mark Campillo wrote:
>>>Boolean (*oldTrap)(FieldPtr, EventPtr);
>>>  //handled = oldTrap(fld, event); <-- This seems to be the problem line
>>
>>Well, honestly the pointer-to-function declaration syntax has always
>>confused me, which is why I prefer typedefs, but I think it's the source of
>>your problem.  The variable oldTrap in this case is a pointer to a
>>function, so you need to dereference it in order to call it.
>>
>>That is, your line should be:
>> handled = (*oldTrap)(fld, event)
>>
>>It would probably be clearer if you did something like this:
>>
>> // create the type
>> typedef Boolean FldHandleEventFunction(FieldPtr, EventPtr);
>>
>> // declare the variable -- explicitly a pointer to a function
>> FldHandleEventFunction *oldTrapP;
>>
>> // get the value
>> FtrGet(myCreator, myResID, &oldTrapP);
>>
>> // call the function
>> handled = (*oldTrapP)(fld, event)
>>
>>
>> --Bob


Reply via email to