### Author Topic: Function Pointer Example 1 Broken  (Read 712 times)

#### Robert

• Hero Member
• Posts: 601
##### Function Pointer Example 1 Broken
« on: January 24, 2020, 03:41:43 PM »
Code: [Select]
`GLOBAL Func(INTEGER) AS FUNCTION INTEGERCALL SetFunc(x0)? Func(2)CALL SetFunc(x1)? Func(2)SUB SetFunc(f(n AS INTEGER) AS FUNCTION INTEGER)  Func = fEND SUB'-----------------------------------------------FUNCTION x0(x AS INTEGER)  FUNCTION = x * xEND FUNCTIONFUNCTION x1(x AS INTEGER)  FUNCTION = x * x * 2END FUNCTION`
Code: [Select]
`SUB SetFunc(f(n AS INTEGER) AS FUNCTION INTEGER)`
is being translated in the prototypes section as

Code: [Select]
`void    SetFunc ((*) (intfunctionint));`
instead of the correct

Code: [Select]
`void    SetFunc (int(*)(int));`
and in the User Subs section as

Code: [Select]
`void SetFunc ((*(void) (int (functionint AS))`
instead of the correct

Code: [Select]
`void SetFunc (int (*f)(int n))`

#### Robert

• Hero Member
• Posts: 601
##### Re: Function Pointer Example 1 Broken
« Reply #1 on: January 24, 2020, 03:49:54 PM »
It was broken in BCX 6.5.0.

BCX 6.2.4. translates it O.K.

#### MrBcx

• Hero Member
• Posts: 786
##### Re: Function Pointer Example 1 Broken
« Reply #2 on: January 24, 2020, 06:41:58 PM »
Robert,

No fix needed, IMO ... it's just that the implementation changed a bit.

Code: [Select]
`GLOBAL Func(DUMMY as INTEGER) AS FUNCTION INTEGERSetFunc(x0)? Func(2)SetFunc(x1)? Func(2)SUB SetFunc (f AS Func_TYPE)   ' <<-- BCX created Func_TYPE for you.   Func = fEND SUB'-----------------------------------------------FUNCTION x0(x AS INTEGER) AS INTEGER  FUNCTION = x * xEND FUNCTIONFUNCTION x1(x AS INTEGER) AS INTEGER  FUNCTION = x * x * 2END FUNCTION`

#### Robert

• Hero Member
• Posts: 601
##### Re: Function Pointer Example 1 Broken
« Reply #3 on: January 24, 2020, 07:26:37 PM »
Thanks for that.

I have no idea where to begin with the warnings and errors in the one below, again from the Help file.

Code: [Select]
`SET Funcs[] AS FARPROC  x0,  x1,  x2,  x3,  x4END SETFUNCTION x0  FUNCTION = 0END FUNCTIONFUNCTION x1  FUNCTION = 1END FUNCTIONFUNCTION x2  FUNCTION = 2END FUNCTIONFUNCTION x3  FUNCTION = 3END FUNCTIONFUNCTION x4  FUNCTION = 4END FUNCTIONSET(PTR Funcs2[])() AS CHAR PTR  s0\$,  s1\$,  s2\$,  s3\$,  s4\$END SETFUNCTION s0\$  FUNCTION = "0"END FUNCTIONFUNCTION s1\$  FUNCTION = "1"END FUNCTIONFUNCTION s2\$  FUNCTION = "2"END FUNCTIONFUNCTION s3\$  FUNCTION = "3"END FUNCTIONFUNCTION s4\$  FUNCTION = "4"END FUNCTIONSET(PTR Funcs3[])(INTEGER) AS INTEGER  n0,  n1,  n2,  n3,  n4END SETFUNCTION n0(n AS INTEGER)  FUNCTION = n * 0END FUNCTIONFUNCTION n1(n AS INTEGER)  FUNCTION = n * 1END FUNCTIONFUNCTION n2(n AS INTEGER)  FUNCTION = n * 2END FUNCTIONFUNCTION n3(n AS INTEGER)  FUNCTION = n * 3END FUNCTIONFUNCTION n4(n AS INTEGER)  FUNCTION = n * 4END FUNCTIONDIM iFOR i = 0 TO 4  ? Funcs[i]()NEXTFOR i = 0 TO 4  ? Funcs2\$[i]()NEXTFOR i = 0 TO 4  ? Funcs3[i](i)NEXT`

#### MrBcx

• Hero Member
• Posts: 786
##### Re: Function Pointer Example 1 Broken
« Reply #4 on: January 24, 2020, 09:15:04 PM »
Robert -- I tackled them one at a time.

They are less confusing and more instructive as 3 individual examples.

Here they are ... tested with LccWin32, Pelles 9, and Mingw

Code: [Select]
` SET Funcs[] AS FARPROC  x0,  x1,  x2,  x3,  x4 END SET FUNCTION x0  FUNCTION = 0 END FUNCTION FUNCTION x1  FUNCTION = 1 END FUNCTION FUNCTION x2  FUNCTION = 2 END FUNCTION FUNCTION x3  FUNCTION = 3 END FUNCTION FUNCTION x4  FUNCTION = 4 END FUNCTION'------------------------ DIM i FOR i = 0 TO 4  ? Funcs[i]() NEXT`

Code: [Select]
`SET Funcs2[] AS FARPROC  s0\$,  s1\$,  s2\$,  s3\$,  s4\$END SETFUNCTION s0\$  FUNCTION = "0"END FUNCTIONFUNCTION s1\$  FUNCTION = "1"END FUNCTIONFUNCTION s2\$  FUNCTION = "2"END FUNCTIONFUNCTION s3\$  FUNCTION = "3"END FUNCTIONFUNCTION s4\$  FUNCTION = "4"END FUNCTION'-----------------------DIM iFOR i = 0 TO 4  ? Funcs2\$[i]()NEXT`

Code: [Select]
`GLOBAL PF_1_ARG(DUMMY as INTEGER) AS FUNCTION INTEGERSET Funcs3[] AS PF_1_ARG_TYPE  n0,  n1,  n2,  n3,  n4END SETFUNCTION n0(n AS INTEGER)  FUNCTION = n * 0END FUNCTIONFUNCTION n1(n AS INTEGER)  FUNCTION = n * 1END FUNCTIONFUNCTION n2(n AS INTEGER)  FUNCTION = n * 2END FUNCTIONFUNCTION n3(n AS INTEGER)  FUNCTION = n * 3END FUNCTIONFUNCTION n4(n AS INTEGER)  FUNCTION = n * 4END FUNCTION'------------------------------DIM iFOR i = 0 TO 4  ? Funcs3[i](i)NEXT`

#### MrBcx

• Hero Member
• Posts: 786
##### Re: Function Pointer Example 1 Broken
« Reply #5 on: April 01, 2021, 02:40:15 PM »
Here are a couple of updated versions of the examples above.

The examples above appear in BCXHelp and should be updated as well.

These compile cleanly with all tested compilers. (Lcc, Pelles, MSVC, GCC, Clang)

Basically, this update replaces the FARPROC definition with the custom typedef that BCX emits.

Code: [Select]
`GLOBAL PTR_Funcs () AS FUNCTION INTEGERSET Funcs[] AS PTR_Funcs_TYPE   ' BCX creates a typedef and tacks on "_TYPE" to the declaration  x0,  x1,  x2,  x3,  x4 END SET FUNCTION x0  FUNCTION = 0 END FUNCTION FUNCTION x1  FUNCTION = 1 END FUNCTION FUNCTION x2  FUNCTION = 2 END FUNCTION FUNCTION x3  FUNCTION = 3 END FUNCTION FUNCTION x4  FUNCTION = 4 END FUNCTION'------------------------ DIM i FOR i = 0 TO 4  ? Funcs[i]() NEXT`
and

Code: [Select]
`GLOBAL PTR_Funcs () AS FUNCTION PCHAR SET Funcs2[] AS PTR_Funcs_TYPE   ' BCX creates a typedef and tacks on "_TYPE" to the declaration  s0\$,  s1\$,  s2\$,  s3\$,  s4\$END SETFUNCTION s0\$  FUNCTION = "0"END FUNCTIONFUNCTION s1\$  FUNCTION = "1"END FUNCTIONFUNCTION s2\$  FUNCTION = "2"END FUNCTIONFUNCTION s3\$  FUNCTION = "3"END FUNCTIONFUNCTION s4\$  FUNCTION = "4"END FUNCTION'-----------------------DIM iFOR i = 0 TO 4  ? Funcs2\$[i]()NEXT'-----------------------`

#### Robert

• Hero Member
• Posts: 601
##### Re: Function Pointer Example 1 Broken
« Reply #6 on: April 01, 2021, 03:16:52 PM »
The examples in the BCX Help file shall be updated.