Tuesday 24 March 2015

Arrays in Arduino

A variable is a label that we give to a piece of information.
This provide us with a simple way to save the information, change it and access it.

when we wanted to name a group of variables of same type, rather than declaring separately we can declare the array of variables.

Arrays are collections of consecutive variables of same type.Each variable in the collection is called an element. The number of elements is called dimensions of the array

array declaration :
one way to declare array without initialization is
int exarray[10];
int is datatype of array
exarray is name of the array
10 is the dimension of array which has a range from 0,1,2,3.... to 9\
if n is the dimension of array it holds the index ranging from 0 to n-1.

another way of declaring array is
int exarray[] = {2,4,5,6,8,10,12,45,78,98};
the above code creates an array of integers with four elements and initializes the each element.

But in the array declaration without initialisation each variable is initialized as 0.

void setup()
{
Serial.begin(9600); // start the serial communication
int exarray[4]; // declare the array with no initial values
int ex2array[] = {4,12,17,19}; // declare the array with initial values
Serial.println("The elements stored in exarray is");
// print the elements in the exarray with index number
for (int i=0;i<4;i++)
{
Serial.print(i);
Serial.print("\t" );
Serial.print(exarray[i]);
Serial.println("");
}

Serial.println("The elements stored in ex2array is");
//print the elements in the ex2array with index number
for (int j=0;j<4;j++)
{
Serial.print(j);
Serial.print("\t");
Serial.print(ex2array[j]);
Serial.println("");
}
}
void loop()
{
//do nothing.
}


and the output is like
array initialization

and surprisingly the elements in the array without initialization are not zeros. all are coming garbage values....


And as Always I go to the Arduino Forum to clarify it. the reply from AWOL says what is the reason for it.


The correct answer is "it depends where you declared the array".
If the array has global scope, or is qualified "static", then it will be initialised (to zero if no initialiser is specified) before "setup()" runs.

If the array has local scope (i.e. it is within a function) and is not qualified "static", then it will not be initialised at all, and its elements will pick up whatever happens to be already there.


which is proved using the below code...

//code for array initialization global
int exarray[4]; // declare the array with no initial values
int ex2array[] = {4,12,17,19}; // declare the array with initial values
void setup()
{
Serial.begin(9600); // start the serial communication
Serial.println("The elements stored in exarray is");
// print the elements in the exarray with index number
for (int i=0;i<4;i++)
{
Serial.print(i);
Serial.print("\t" );
Serial.print(exarray[i]);
Serial.println("");
}

Serial.println("The elements stored in ex2array is");
//print the elements in the ex2array with index number
for (int j=0;j<4;j++)
{
Serial.print(j);
Serial.print("\t");
Serial.print(ex2array[j]);
Serial.println("");
}
}
void loop()
{
//do nothing.
}


Output:
array initialization global

you can see the forum post here