C function typedef

New to FreeBASIC? Post your questions here.
Post Reply
lal0qnsc
Posts: 14
Joined: Apr 05, 2022 10:27

C function typedef

Post by lal0qnsc »

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.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: C function typedef

Post by dodicat »

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


 
lal0qnsc
Posts: 14
Joined: Apr 05, 2022 10:27

Re: C function typedef

Post by lal0qnsc »

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


 
Still have no idea how to translate typedef int foo(int x, int y);
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: C function typedef

Post by TJF »

lal0qnsc wrote: Apr 06, 2022 14:33typedef int foo(int x, int y);
is a function prototype and translates to

Code: Select all

TYPE foo AS FUNCTION(BYVAL AS LONG, BYVAL AS LONG)AS LONG
Note: int (32 bit in C) translates to LONG in order to work on 32- and 64-bit FB code.

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.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: C function typedef

Post by dodicat »

Are you translating some C code, or are you wanting to use function pointers in Freebasic?

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

and TJF's comments about data type integer, so maybe use long instead of integer
long always 32 bits
longint always 64 bits
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: C function typedef

Post by TJF »

Sorry, h_2_bi output is

Code: Select all

' typedef int foo(int x, int y);
TYPE foo AS FUNCTION CDECL(BYVAL AS LONG, BYVAL AS LONG) AS LONG
CDECL takes care of the matching calling convention. You can omit it by enclosing the TYPE in an EXTERN "C" ... END EXTERN block.
Post Reply