I encounter code like this in C library: typedef int foo(int x, int y);
What does the declaration in FreeBASIC look like?
I have read both this (https://riptutorial.com/c/example/31818 ... n-pointers) and this (https://www.freebasic.net/wiki/TutPterDataType) but it's still not clear to me.
C function typedef
Re: C function typedef
Freebasic is actually a bit easier to write and read than C for this.
(Which is the fundamental principle of freebasic)
(Which is the fundamental principle of freebasic)
Code: Select all
sub print_to_n(n as integer)
print __function__
for i as integer = 1 to n
print i
next
end sub
sub print_n(n as integer)
print __function__
for i as integer = 1 to n
print i
next
end sub
type printer_t as sub(as integer)
dim as printer_t p
p=@print_to_n
p(5)
p=@print_n
p(5)
sleep
Re: C function typedef
Still have no idea how to translate typedef int foo(int x, int y);dodicat wrote: ↑Apr 06, 2022 13:34 Freebasic is actually a bit easier to write and read than C for this.
(Which is the fundamental principle of freebasic)Code: Select all
sub print_to_n(n as integer) print __function__ for i as integer = 1 to n print i next end sub sub print_n(n as integer) print __function__ for i as integer = 1 to n print i next end sub type printer_t as sub(as integer) dim as printer_t p p=@print_to_n p(5) p=@print_n p(5) sleep
Re: C function typedef
is a function prototype and translates to
Code: Select all
TYPE foo AS FUNCTION(BYVAL AS LONG, BYVAL AS LONG)AS LONG
Perhaps you want to check h_2_bi for such translations. Install it in Geany IDE as custom command in order to translate statement by statement.
Re: C function typedef
Are you translating some C code, or are you wanting to use function pointers in Freebasic?
and TJF's comments about data type integer, so maybe use long instead of integer
long always 32 bits
longint always 64 bits
Code: Select all
type foo as function(x as integer,y as integer) as integer
function dothis(x as integer,y as integer) as integer
return x+y
end function
function dothat(x as integer,y as integer) as integer
return x*y
end function
sub usefoo(f as foo, y as integer)
y=f(2,3)+y
print y
end sub
dim as foo x '(x is a function pointer)
x=@dothis
usefoo(x,10)
x=@dothat
usefoo(x,10)
sleep
long always 32 bits
longint always 64 bits
Re: C function typedef
Sorry, h_2_bi output isCDECL takes care of the matching calling convention. You can omit it by enclosing the TYPE in an EXTERN "C" ... END EXTERN block.
Code: Select all
' typedef int foo(int x, int y);
TYPE foo AS FUNCTION CDECL(BYVAL AS LONG, BYVAL AS LONG) AS LONG