Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/18/24 10:45
StartWeek not working as it should
by Zheka. 04/18/24 10:11
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 600 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, 11honza11, ccorrea, sakolin, rajesh7827
19046 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
me #482317
01/27/21 15:47
01/27/21 15:47
Joined: May 2015
Posts: 390
Czech Republic
G
Grat Offline OP
Senior Member
Grat  Offline OP
Senior Member
G

Joined: May 2015
Posts: 390
Czech Republic
Is this correct?

Code
	var data[20]={0};           <---- inicializace with 0
	data[0]=2.1;
	data[19]=19.0;
	printf("\n%f %f",data[0],data[19]);

	memset(data,0,sizeof(var)*20);     <---- set all cell in data to 0 
	printf("\n%f %f",data[0],data[19]);  < --- looks OK

Re: me [Re: Grat] #482319
01/27/21 17:01
01/27/21 17:01
Joined: Feb 2017
Posts: 1,725
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,725
Chicago
No. Lite-C does not support array initializers. Use memset instead.

EDIT: Actually, it does support array initialization, but it causes arrays to become static.
https://zorro-project.com/manual/en/aarray.htm

C++ does support local array initialization.

Code
void do_foo(){
	int foo[5]={0};
	int i=0;
	for(i=0;i<5;i++){
		printf("\n[%d] %d",i,foo[i]);
		foo[i]=999;
	}
}

void main(void) 
{
	do_foo();
	do_foo();
}

/*
output:
[0] 0
[1] 0
[2] 0
[3] 0
[4] 0
[0] 999
[1] 999
[2] 999
[3] 999
[4] 999
*/

Last edited by AndrewAMD; 01/28/21 15:50.
Re: me [Re: Grat] #482335
01/28/21 15:29
01/28/21 15:29
Joined: May 2015
Posts: 390
Czech Republic
G
Grat Offline OP
Senior Member
Grat  Offline OP
Senior Member
G

Joined: May 2015
Posts: 390
Czech Republic
So,

this is a new for me. Local array variable is declared like static?

Code
	static int foo[5]={0};
and
	int foo[5]={0};


is the same?

Re: me [Re: Grat] #482336
01/28/21 15:36
01/28/21 15:36
Joined: Feb 2017
Posts: 1,725
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,725
Chicago
In Lite-C, yes. Or at least, the manual says that the array becomes static if the array is initialized.
Quote
Initializing in the definition works only for simple arrays; multidimensional arrays, arrays that contain strings, structs, or anything else than numbers, or several arrays defined together in a single logical line must be initialized by explicitly setting their elements. Initializing local arrays makes them static (see below), meaning that they keep their previous values when the function is called the next time. For initializing them every time the function is called, explicitly set them to their initial values, like this:

Code
function foo()
{
  var my_vector[3];
  my_vector[0] = 10;
  my_vector[1] = 20;
  my_vector[2] = 30;
  ...
  var my_static[3] = { 10, 20, 30 }; // initializing local arrays makes them static 
  ...
}

https://zorro-project.com/manual/en/aarray.htm


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1