Do you know how to create an array of variables?
A string is simply an array of characters, terminated with a null character '\0'.
For instance, this code will declare a character array with 6 elements:
Each element can contain a character, and are indexed from zero to "n-1"
Code:
cArray[0] = 'H'; // <--- note a single character is contained within single quotes
cArray[1] = 'e';
cArray[2] = 'l';
cArray[3] = 'l';
cArray[4] = 'o';
cArray[5] = '\0';
// There are six elements in this array, numbered 0 - 5.
// Some new to this mistakenly try to access "cArray[6]" - ** this is out of bounds! **
There are other ways to define a string. For instance, we could have done this:
Code:
char cArray[6] = "Hello"; // <--- note string of characters is contained within double quotes
Note that "Hello" only has five characters, but our array has six elements. This is because we need to leave space at the end of the string for the null character (which is appended automatically when a string is declared like this).
Play around with this for a little bit until you feel comfortable, then we can go over the next step.
[edit] Perhaps I should include some printing techniques:
Code:
// printing one character at a time
for(i=0; i<6; i++)
printf("%c",cArray[i]); // <--- %c prints a single character
Code:
// print entire string at once
printf("%s",cArray); // <--- %s prints a null-character-terminated string
// note that just the array name is given without any brackets/indices