Gamestudio Links
Zorro Links
Newest Posts
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
Release 2.68 replacement of the .par format
by Martin_HH. 09/23/25 20:48
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
1 registered members (dBc), 17,435 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 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,806
Chicago
AndrewAMD Offline
Serious User
AndrewAMD  Offline
Serious User

Joined: Feb 2017
Posts: 1,806
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,806
Chicago
AndrewAMD Offline
Serious User
AndrewAMD  Offline
Serious User

Joined: Feb 2017
Posts: 1,806
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