Character Arrays¶
Strings: Mistake by Design?¶
Only what is necessary is built-in in C
From today’s point of view C is the language for hardware-oriented programming
Invented to keep UNIX portable, independent from PDP-11 assembler
⟶ C itself is the language core - everything else belongs in libraries
Contradiction:
Language core knows what string literals are
7-bit ASCII sufficed at that time ⟶ no multibyte character sets, no need for Unicode
But: much later somebody claimed that “640K is enough”
Strings: Definition¶
String
Array of characters …
… terminated by a “null” character (
'\0'
)
char a_string[] = "hello";
|
Strings: Library Functions¶
Functions from the standard library
strlen(const char[])
strcpy(char dest[], const char src[])
strncpy(char dest[], const char src[], int maxlen)
strcat(char dest[], const char src[])
strncat(char dest[], char src[], int n)
strcmp(const char lhs[], const char rhs[])
strncmp(const char lhs[], const char rhs[], int maxlen)
Many more ⟶ see manual page
Strings as Parameters¶
Strings (like arrays in general) are passed as pointers
⟶ Modifications visible to the caller
char a_string[] = "hello";
char another_string[10];
...
copy(another_string, a_string);
|
Strings: Dangers¶
Low level definition leads to errors
Copy: not enough memory allocated to hold the copy
Forget to null-terminate when composing strings by hand
… many many more …