Author Topic: Slick-C Function Pointers  (Read 8475 times)

outsider

  • Community Member
  • Posts: 64
  • Hero Points: 1
Slick-C Function Pointers
« on: August 06, 2011, 03:35:03 PM »
Hi guys,

Maybe I'm being dumb or maybe the Slick-C documentation could be off (surely not!) but I cannot get the function pointer syntax to compile.

Anybody have a working example of this?

Docs say;
Code: [Select]
  [static] TypeName (*variable1)([ArgDecl1, ArgDecl2,...]){=function_name};

So my effort looks like (creating a pointer to the message() function for example);

Code: [Select]
void (*funcPtr)( _str myMsg  ){="message"};
On compiling the module I get;

Expecting ';'

Any ideas?

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Slick-C Function Pointers
« Reply #1 on: August 06, 2011, 08:40:00 PM »
Did you try without the quotes?

outsider

  • Community Member
  • Posts: 64
  • Hero Points: 1
Re: Slick-C Function Pointers
« Reply #2 on: August 06, 2011, 09:03:48 PM »
yep!

That and a gazillion other variations. Same error message.

Graeme

  • Senior Community Member
  • Posts: 2821
  • Hero Points: 347
Re: Slick-C Function Pointers
« Reply #3 on: August 07, 2011, 02:58:20 AM »
Search for (* in slick c macro sources.

You could try typeless as TreeShiftSelect3 does or maybe like this

void (*pfnReplaceWord)(_str insertWord,_str prefix,int &removeStartCol,int &removeLen,boolean onlyInsertWord)= gAutoCompleteResults.words[word_index].pfnReplaceWord;

Looks like the braces shown in the help are not supposed to be there.


outsider

  • Community Member
  • Posts: 64
  • Hero Points: 1
Re: Slick-C Function Pointers
« Reply #4 on: August 07, 2011, 11:30:19 AM »
Thanks for the tip Graham.

I couldn't find a single example of an actual function pointer assignment (excluding assignment to null) in the macro sources. All the definitions of the function pointers themselves are formal parameters created in the function signature.

I got this to work - partially - but I can't get it to work for built-ins.

For future reference, here is an example;

Code: [Select]
int (*funcPtr)( int a, int b ) = add;

int add( int x, int y)
{
   return x + y;
}

Clearly this isn't rocket science and it's what you'd expect from regular C syntax, but it is NOT what was in the documentation.

Any idea on how to get this to work with built-ins? In the meantime I could write a wrapper I suppose.

Thanks for all your input guys.

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Slick-C Function Pointers
« Reply #5 on: August 07, 2011, 09:13:23 PM »
I couldn't find a single example of an actual function pointer assignment (excluding assignment to null) in the macro sources.

I used the following UNIX style regex:  "\([^)]*\*[^(]*\)\("
It found tons of declarations and usages of function pointers.
To find assignments, one could iteratively use Ctrl+/ to find the references to those matches.
The syntax and usage looks the same as C, from what I can see.

Don't know about builtins; maybe there are some instances of that in the 392 matches from the regex above.

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Slick-C Function Pointers
« Reply #6 on: August 07, 2011, 09:25:33 PM »
Aha.  Assuming you're on Windows:  in SE choose Help|Search... then enter "function pointer assignment" in the Search box and hit Enter.  From the list of matches, pick "Functions".  There are many examples of the syntax in that topic.

And clicking on the "Function Prototypes" link at the top of the topic jumps to a section that mentions this:
Quote
    int proc(_str s,_str list[]);         // Function prototype.
    int (*pfn)(_str s,_str list[])=proc;  // Pointer to function.
    _command void command1(...);          // Function prototype.
    _command void command1(...) {         // Must have ... here to match prototype.
                                          // Use arg function here to get or set
                                          // arguments.
    }

Worst case, if you can't figure out how to get a pointer to a builtin, you can write a one-line wrapper function that calls the builtin, and instead get a pointer to the wrapper function.

outsider

  • Community Member
  • Posts: 64
  • Hero Points: 1
Re: Slick-C Function Pointers
« Reply #7 on: August 08, 2011, 03:38:09 PM »
Hi chrisant

I used Ctrl+. to try and jump to definitions. Didn't find anything useful. Regex would have helped though - clearly I need to brush up on it.  :)

And yes, my solution was found by eventually finding the help article on function prototypes that you cited above - my point was that it contradicted the first article I came across.

Anyway - I really appreciate all your input. You and Graeme and hs2 and others add a lot of value to this board.

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: Slick-C Function Pointers
« Reply #8 on: August 08, 2011, 08:10:08 PM »
I used Ctrl+. to try and jump to definitions. Didn't find anything useful.

Right, definitions aren't helpful for this.
Use Ctrl+/ to list the references, that would be more relevant.