me

Posted By: Grat

me - 01/27/21 15:47

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
Posted By: AndrewAMD

Re: me - 01/27/21 17:01

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
*/
Posted By: Grat

Re: me - 01/28/21 15:29

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?
Posted By: AndrewAMD

Re: me - 01/28/21 15:36

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
© 2024 lite-C Forums