Can somebody tell me what differences is between 2 below code.
The first code is OK.
But second Code has an error: error: variable-sized object 'b' may not be initialized variable-sized object 'b' may not be initialized.
I'm thinking its because declaring a variable doesn't make it an instance of the variable. As a matter of fact, no storage is allocated by the compiled code unless a declared variable is actually used in 'active' code somewhere (a function, or in setup() or loop() ). I believe that what is happening is (even though you are assigning a default value for 'a'), the compiler sees that as a placeholder.
int a=50; // compiler will create this variable later
char b[a]={0}; // so "a" is a place holder and the value cannot yet be used
One way it can be done up in the declaration area is to use a #define, like:
#define a 50
char b(a)={0};
And that's not a good way, but it is a way.
The best way to trick the compiler is:
const int a=50;
char b[a]={0};
Which should compile.
Your original code gives me a different error in the Arduino 1.6.4 IDE:
error: array bound is not an integer constant before ']' token
array bound is not an integer constant before ']' token
memset is faster but if 'b' is an array of objects and not an array of simple integer type then the results could be wrong since the object constructors won't be called.