Gamestudio Links
Zorro Links
Newest Posts
MT5 bridge not working on MT5 v. 5 build 4160
by EternallyCurious. 04/25/24 20:49
Data from CSV not parsed correctly
by EternallyCurious. 04/25/24 10:20
Trading Journey
by howardR. 04/24/24 20:04
M1 Oversampling
by Petra. 04/24/24 10:34
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Scripts not found
by juergen_wue. 04/20/24 18:51
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (EternallyCurious, AndrewAMD, TipmyPip, Quad), 902 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mega_Rod, EternallyCurious, howardR, 11honza11, ccorrea
19048 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
How to declare/initialize an array of function pointers #485834
04/27/22 14:17
04/27/22 14:17
Joined: Jul 2017
Posts: 784
Z
Zheka Offline OP
User
Zheka  Offline OP
User
Z

Joined: Jul 2017
Posts: 784
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).

Re: How to declare/initialize an array of function pointers [Re: Zheka] #485836
04/27/22 15:12
04/27/22 15:12
Joined: Feb 2017
Posts: 1,725
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,725
Chicago
Just make an array of DWORDs and then assign it any given function pointer to a function prototype on demand.

Last edited by AndrewAMD; 04/27/22 16:11.
Re: How to declare/initialize an array of function pointers [Re: Zheka] #485837
04/27/22 15:59
04/27/22 15:59
Joined: Jul 2017
Posts: 784
Z
Zheka Offline OP
User
Zheka  Offline OP
User
Z

Joined: Jul 2017
Posts: 784
So, there is no way to bulk-initialize it at declaration?

Re: How to declare/initialize an array of function pointers [Re: Zheka] #485838
04/27/22 16:06
04/27/22 16:06
Joined: Feb 2017
Posts: 1,725
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,725
Chicago
Try it, it should work.

Re: How to declare/initialize an array of function pointers [Re: Zheka] #485839
04/27/22 16:36
04/27/22 16:36
Joined: Jul 2017
Posts: 784
Z
Zheka Offline OP
User
Zheka  Offline OP
User
Z

Joined: Jul 2017
Posts: 784
DWORD f[3];
....
...
f[0]=SMA; f[1]=EMA; - works. Is that what you mean?

Re: How to declare/initialize an array of function pointers [Re: Zheka] #485840
04/27/22 16:49
04/27/22 16:49
Joined: Feb 2017
Posts: 1,725
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,725
Chicago
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!
*/

Re: How to declare/initialize an array of function pointers [Re: Zheka] #485842
04/27/22 17:39
04/27/22 17:39
Joined: Jul 2017
Posts: 784
Z
Zheka Offline OP
User
Zheka  Offline OP
User
Z

Joined: Jul 2017
Posts: 784
Yes, thank you.

Re: How to declare/initialize an array of function pointers [Re: Zheka] #485843
04/27/22 17:42
04/27/22 17:42
Joined: Apr 2008
Posts: 586
Austria
Petra Offline
Support
Petra  Offline
Support

Joined: Apr 2008
Posts: 586
Austria
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.

Re: How to declare/initialize an array of function pointers [Re: Zheka] #485869
05/01/22 12:04
05/01/22 12:04
Joined: Jul 2017
Posts: 784
Z
Zheka Offline OP
User
Zheka  Offline OP
User
Z

Joined: Jul 2017
Posts: 784
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 Files
ema047fptr.PNG (39 downloads)
Re: How to declare/initialize an array of function pointers [Re: Zheka] #485878
05/02/22 09:15
05/02/22 09:15
Joined: Apr 2008
Posts: 586
Austria
Petra Offline
Support
Petra  Offline
Support

Joined: Apr 2008
Posts: 586
Austria
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.

Re: How to declare/initialize an array of function pointers [Re: Zheka] #485880
05/02/22 12:43
05/02/22 12:43
Joined: Jul 2017
Posts: 784
Z
Zheka Offline OP
User
Zheka  Offline OP
User
Z

Joined: Jul 2017
Posts: 784
OK,clear. But why would it still work for alpha<=0.37?

Re: How to declare/initialize an array of function pointers [Re: Zheka] #485881
05/02/22 12:48
05/02/22 12:48
Joined: Feb 2017
Posts: 1,725
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,725
Chicago
Probably undefined behavior that happened to work. It's usually best to avoid undefined behavior in the first place.

Re: How to declare/initialize an array of function pointers [Re: Zheka] #485882
05/02/22 13:08
05/02/22 13:08
Joined: Jul 2017
Posts: 784
Z
Zheka Offline OP
User
Zheka  Offline OP
User
Z

Joined: Jul 2017
Posts: 784
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...

Last edited by Zheka; 05/02/22 13:12.
Re: How to declare/initialize an array of function pointers [Re: Zheka] #485883
05/02/22 13:09
05/02/22 13:09
Joined: Feb 2017
Posts: 1,725
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,725
Chicago
At compile time, your function prototype is wrong. This is why you need a wrapper.

Re: How to declare/initialize an array of function pointers [Re: Zheka] #485884
05/02/22 13:26
05/02/22 13:26
Joined: Jul 2017
Posts: 784
Z
Zheka Offline OP
User
Zheka  Offline OP
User
Z

Joined: Jul 2017
Posts: 784
Ah, ok, tx!

Page 1 of 2 1 2

Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1