Arrays and Strings
Character Arrays
C strings are arrays of characters ending with a null terminator.
null terminator
The `'\0'` character marks where a C string ends.
mutable characters
Changing one array element changes the visible string content.
Character Arrays
char_arrays.c
Replay: real traced execution (multi-file project)
#include <stdio.h>
int main(void) {
char word[4] = {'c', 'a', 't', '\0'};
char first = 'C';
word[0] = first;
printf("word=%s\n", word);
return 0;
}
#include <stdio.h>
int main(void) {
char word[4] = {'c', 'a', 't', '\0'};
char first = 'B';
word[0] = first;
printf("word=%s\n", word);
return 0;
}
#include <stdio.h>
int main(void) {
char word[4] = {'c', 'a', 't', '\0'};
char first = 'R';
word[0] = first;
printf("word=%s\n", word);
return 0;
}
word ← cat, first ← C, word[0] ← C
3int main(void) {4 char word→ cat[4] = {'c', 'a', 't', '\0'};5 char first→ C = 'C'; //@first='B', 'R'67 word[0]→ C = firstC;89 printf("word=%s\n", wordCat);10 return 0;11}outputword=Cat
word ← cat, first ← B, word[0] ← B
3int main(void) {4 char word→ cat[4] = {'c', 'a', 't', '\0'};5 char first→ B = 'B';67 word[0]→ B = firstB;89 printf("word=%s\n", wordBat);10 return 0;11}outputword=Bat
word ← cat, first ← R, word[0] ← R
3int main(void) {4 char word→ cat[4] = {'c', 'a', 't', '\0'};5 char first→ R = 'R';67 word[0]→ R = firstR;89 printf("word=%s\n", wordRat);10 return 0;11}outputword=Rat