char array

Hi!

can someone plz explain me in detail how this works? (The bold stuff)

char stCurrent[20]="";
int stCurrentLen=0;

void updateStr(int val)
{
if (stCurrentLen<20)
{
stCurrent[stCurrentLen]=val;
** stCurrent[stCurrentLen+1]=’\0’;**
** stCurrentLen++;**
myGLCD.setColor(0, 255, 0);
myGLCD.print(stCurrent, LEFT, 224);

and if you write updateStr(‘1’); 1 is written

the code is taken from Henning Karlsens Utouch library, the button_test example

read - http://www.cplusplus.com/doc/tutorial/ntcs/

Than you should be able to understand

char stCurrent[20]="";  //declare an array of 20 chars
int stCurrentLen=0;    //declare a variable holding the current length of the string

void updateStr(int val)  
{
  if (stCurrentLen<20)  //if we are not already at the end of the array
  {
    stCurrent[stCurrentLen]=val;  //put the value of val at the next position in the array
    stCurrent[stCurrentLen+1]='\0';  //terminate the array with a null to signify the end of the string
    stCurrentLen++;  //update the array index to the next position
    myGLCD.setColor(0, 255, 0);  //position the cursor
    myGLCD.print(stCurrent, LEFT, 224);  //print the string to the screen.

Thanks guys!!

last thing: char stCurrent[20]=""; .... what does "" do? couldn't it just be char stCurrent[20]?

lmfao: Thanks guys!!

last thing: char stCurrent[20]=""; .... what does "" do? couldn't it just be char stCurrent[20]?

It is redundant, does nothing. But it may make the initialization more obvious to the reader.

@aarg: I thought the statement:

char stCurrent[20]="";

was different because it set the first element of the array to null. If the array is defined without any initializer and has local scope, the content of the first element is unknown and things like strcat() may not work properly. If the array has global scope and the compiler initializes global arrays to 0, then it wouldn't matter.