Welcome, Guest. Please login or register.

Author Topic: function typecast help  (Read 950 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline jahcTopic starter

  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 521
    • Show only replies by jahc
    • http://wookiechat.amigarevolution.com
function typecast help
« on: February 21, 2004, 06:15:35 AM »
I dont know how to typecast to "ULONG(*)()".. can someone help me out here?

const struct Hook ConstructLI_TextHook = { { NULL,NULL }, (ULONG*)ConstructLI_TextFunc, NULL,NULL };


8.dh0:MUI/Developer/C/Examples/a> g++ a.cpp
In file included from a.cpp:74:
NList-Demo2.c:109: initialization to `ULONG (*)()' from `ULONG *'

....
I dont know if this helps but heres the Hook function for the code above:

#ifdef __SASC
SAVEDS ASM APTR ConstructLI_TextFunc(REG(a0) struct Hook *hook, REG(a2) Object *obj, REG(a1) struct NList_ConstructMessage *ncm)
{
#endif
#ifdef __GNUC__
static APTR ConstructLI_TextFunc(void)
{ register Object *a2 __asm("a2");                        Object *obj = a2;
  register struct NList_ConstructMessage *a1 __asm("a1"); struct NList_ConstructMessage *ncm = a1;
  register struct Hook *a0 __asm("a0");                   struct Hook *hook = a0;
#endif

  struct LITD *new_entry = (struct LITD *) AllocVec(sizeof(struct LITD),0);
  if(new_entry)
  { int i = 0, j = 0;
    new_entry->num = -1;
    new_entry->str2 = (char *) ncm->entry;
    while ((j < 3) && new_entry->str2)
    { if ((new_entry->str2 > 'A') && (new_entry->str2 < 'z'))
        new_entry->str1[j++] = new_entry->str2;
      if (new_entry->str2 == '\033')
        i++;
      i++;
    }
    new_entry->str1[j] = '\0';

    return (new_entry);
  }
  return (NULL);
}


Thanks guys.
 

Offline Thomas

Re: function typecast help
« Reply #1 on: February 21, 2004, 05:47:17 PM »

Typecasting to a generic pointer should do the trick in most cases. Try (void *).

Bye,
Thomas

Offline jahcTopic starter

  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 521
    • Show only replies by jahc
    • http://wookiechat.amigarevolution.com
Re: function typecast help
« Reply #2 on: February 21, 2004, 06:36:57 PM »
Nope that doesnt work..

In file included from a.cpp:74:
NList-Demo2.c:109: ANSI C++ forbids implicit conversion from `void *' in initialization

-EDIT- (APTR) doesnt work either. Thats what it was set to in the beginning.

any other ideas? :/
 

Offline Noster

  • Sr. Member
  • ****
  • Join Date: Jan 2004
  • Posts: 375
    • Show only replies by Noster
Re: function typecast help
« Reply #3 on: February 21, 2004, 09:30:35 PM »
Hi

First of all, why don't you deklare your hook function in the desired type ?

#ifdef __SASC
SAVEDS ASM ULONG ConstructLI_TextFunc(REG(a0) struct Hook *hook, REG(a2) Object *obj, REG(a1) struct NList_ConstructMessage *ncm)
{
#endif
#ifdef __GNUC__
static ULONG ConstructLI_TextFunc(void)
...


Second, if you wish to cast a function-pointer, you must also specify all arguments like this:

APTR foobar (ULONG arg1, UBYTE arg2)
{
   /* This function expects two arguments and returns an APTR.
    */
   APTR result;

   /* Do anything with the arguments and 'result'...
    */
...

  return result;
}

void f ()
{
   /* This function want's to cast a pointer to the function "foobar" defined above
    * to a function pointer that expects a different returnvalue.
    */
   ULONG(*fctptr)(ULONG,UBYTE);   /* this is a functionpointer */

   /* Now we cast a pointer to "foobar" to the type required for the function pointer:
    */
   fctPtr = (ULONG(*)(ULONG,UBYTE)foobar;

   /* That's it, now you could call "foobar" using the function pointer 'fctptr'...
    */
...

   Noster
DON\\\'T PANIC
    Douglas Adams - Hitch Hiker\\\'s Guide to the Galaxis
 

Offline jahcTopic starter

  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 521
    • Show only replies by jahc
    • http://wookiechat.amigarevolution.com
Re: function typecast help
« Reply #4 on: February 22, 2004, 02:12:09 AM »
thanks man, got it sorted now. this is the code that actually got it all working in the end:

ULONG(*testPtr)() = (ULONG(*)())ConstructLI_TextFunc;
const struct Hook ConstructLI_TextHook = { { NULL,NULL }, testPtr, NULL,NULL };

:) took a bit of fiddling. again, thanks for your help.

-EDIT- AHA.. this works too: const struct Hook ConstructLI_TextHook = { { NULL,NULL }, (ULONG(*)())ConstructLI_TextFunc, NULL,NULL };