How to declare/initialize an array of function pointers

Posted By: Zheka

How to declare/initialize an array of function pointers - 04/27/22 14:17

How can one declare and initialize an array of function pointers in lite-c?

var (*f[3])(vars,int) = {SMA, EMA,ZMA}; gives a syntax error (also with non-Zorro functions).
Posted By: AndrewAMD

Re: How to declare/initialize an array of function pointers - 04/27/22 15:12

Just make an array of DWORDs and then assign it any given function pointer to a function prototype on demand.
Posted By: Zheka

Re: How to declare/initialize an array of function pointers - 04/27/22 15:59

So, there is no way to bulk-initialize it at declaration?
Posted By: AndrewAMD

Re: How to declare/initialize an array of function pointers - 04/27/22 16:06

Try it, it should work.
Posted By: Zheka

Re: How to declare/initialize an array of function pointers - 04/27/22 16:36

DWORD f[3];
....
...
f[0]=SMA; f[1]=EMA; - works. Is that what you mean?
Posted By: AndrewAMD

Re: How to declare/initialize an array of function pointers - 04/27/22 16:49

Come to think of it, that's the only way you can do it because Lite-C does not allow you to initialize with variables.
Code
int fx_add(int a,int b){return a+b;}
int fx_subtract(int a,int b){return a-b;}
int fx_multiply(int a,int b){return a*b;}
int fx_divide(int a,int b){return a/b;}
int fx(int a,int b); //prototype
int do_fx(DWORD ptr, int a, int b){fx=ptr; return fx(a,b);}
void main(){
	set(LOGFILE);
	DWORD fptrs[4];
	fptrs[0]=fx_add;
	fptrs[1]=fx_subtract;
	fptrs[2]=fx_multiply;
	fptrs[3]=fx_divide;
	int i;
	for(i=0;i<4;i++){
		int a=10,b=2;
		int out = do_fx(fptrs[i],a,b);
		printf("\n[%d](%d,%d)=%d",i,a,b,out);
	}
	printf("\nDone!");
}

/* OUTPUT:
[0](10,2)=12
[1](10,2)=8
[2](10,2)=20
[3](10,2)=5
Done!
*/
Posted By: Zheka

Re: How to declare/initialize an array of function pointers - 04/27/22 17:39

Yes, thank you.
Posted By: Petra

Re: How to declare/initialize an array of function pointers - 04/27/22 17:42

For the indicators there is already a list of function pointers: g->Functions. It has all functions in the order of appearance in functions.h.
Posted By: Zheka

Re: How to declare/initialize an array of function pointers - 05/01/22 12:04

On a separate note:
Code
var myma (vars,var);

var ma( DWORD func, vars prc, var len) {	
	myma=func;
	return  myma(prc,len);
}

void run () {	
	var sma = ma(EMA,seriesC(),0.47);
	
	watch("!ma=", sma, EMA(seriesC(),0.47));		
}
The 1st ema call fails for all alpha >0.37, the 2nd - always works. Why and how to make it work?

Attached picture ema047fptr.PNG
Posted By: Petra

Re: How to declare/initialize an array of function pointers - 05/02/22 09:15

Because EMA is overridden. You cannot directly pass it as a pointer. But you can wrap it and then use a pointer to the wrapper.
Posted By: Zheka

Re: How to declare/initialize an array of function pointers - 05/02/22 12:43

OK,clear. But why would it still work for alpha<=0.37?
Posted By: AndrewAMD

Re: How to declare/initialize an array of function pointers - 05/02/22 12:48

Probably undefined behavior that happened to work. It's usually best to avoid undefined behavior in the first place.
Posted By: Zheka

Re: How to declare/initialize an array of function pointers - 05/02/22 13:08

Just to understand: overloaded function resolution happens at compile time, and the sample code is 'deterministic' with respect to EMA(). Why would it then be 'undefined' behavior?


Btw: if the 2nd parameter is an int, rather than a var, all works fine...
Posted By: AndrewAMD

Re: How to declare/initialize an array of function pointers - 05/02/22 13:09

At compile time, your function prototype is wrong. This is why you need a wrapper.
Posted By: Zheka

Re: How to declare/initialize an array of function pointers - 05/02/22 13:26

Ah, ok, tx!
© 2024 lite-C Forums