String - char array

From Microduino Wiki
Revision as of 05:15, 22 August 2016 by Fengfeng (talk) (Created page with "string<br> *'''Description'''<br> There are two kinds of text strings. You can use the string data type(the core of the version 0019), or you can make a string, which is...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

string

  • Description

There are two kinds of text strings. You can use the string data type(the core of the version 0019), or you can make a string, which is composed of char arrays and null-terminated character('\0'). String object will bring more functions, and also consume more memory resource at the same time.

  • Example

The following strings are all effective statement.

  char Str1[15];
  char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
  char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};
  char Str4[ ] = "arduino";
  char Str5[8] = "arduino";
  char Str6[15] = "arduino";


The interpretation of the statement strings<br>

In Str1, declare an uninitialized char array. <br>
In Str2, declare an char array(include an additional character), and the compiler will automatically add the required null characters. <br>
In Str3, it is clear to add the null character. <br>
In Str4, use quotation marks to separate the initialized string constants, and the compiler will adjust the size of the array to adapt to the string constants and terminate the null characters. <br>
In Str5, initialize an array including a clear size and a string constant. <br>
In Str6, initialize the array, and reserve the extra space for a larger string. <br>

Null-terminated character

Generally speaking, there is a null-terminated character (0 in ASCII code)at the end of a string, in order to let the powerful function know the end of a string. Otherwise, they will continue to read the following bytes from the memory, which doesn’t belong to the part of the string.

This means that your string includes more character space than that of the text you want. This is why Str2 and Str5 need eight characters, even though “Arduino” has only seven characters - the last position will automatically be filled up with null characters, and str4 will be automatically adjusted to eight characters, including an extra empty. In Str3, we have specifically included the null character (have written '\ 0').

It is important to note that the string may have not the last null character(for example, in Str2, you have defined the length of the string as 7, rather than 8), and this will destroy most functions of using strings, so don’t do it deliberately. If you notice some strange phenomena(the operators in the string), this is basically caused by this reason.
Single quotation mark?Or double quotation mark?

When defining the string, double quotation mark is used(for example, “ABC”), while defining a single character, single quotation mark is needed(for example, 'A').
Pack long string

You can pack the long string as this: char myString[] = “This is the first line” ” this is the second line” ” etcetera”;
String array

When your application contains a lot of words, such as a project with a LCD screen, it is very handy to create a string array. Because the string itself is an array, it is actually the typical of a two dimensional array.

In the following code, of ”char*”, a '*' at the end of char represents this is a “pointer” array. All array names are actually pointers, so so it needs an array of an array. The pointer is one of the very deep part for the beginner of the C language, and we can apply it effectively without understanding it in detail.

  • Example
char* myStrings[]={
  "This is string 1", "This is string 2", "This is string 3",
  "This is string 4", "This is string 5","This is string 6"};
 
void setup(){
  Serial.begin(9600);
}
 
void loop(){
  for (int i = 0; i < 6; i++){
    Serial.println(myStrings[i]);
    delay(500);
  }
}

[Return to Arduino Syntax Manual]