Lecture 4: C Strings
Chars
How can we represent chars in binary (and subsequently strings)?
- Basically, stored in ASCII which maps chars to integers. These integers are then stored in binary. As a result, we can have operators like equality.
- Sequentially ordered (lowercase a is 97 and lowercase b is 98).
- Lowercase letters are 32 more than their uppercase equivalents (bit flip!)
- Bit flipping the 32's bit.
Strings
- An array of characters; no designated data type unlike char in C.
- How do we differ? Via a null terminator. Tells when string stops. Thus, always need to allocate extra space in the array for it.
- Strings are not objects so they do not embed additional information such as its length.
- Passed into functions and such via pointer reference (to the first char) so no need to copy.
- We can still use char* and char[] in the same way though.
Concatenation and substrings
- Can't perform these two operations using HLPL commands such as
+
. Since it stores addresses.
- As such, we use a library function called
strcat
.- Removes old null terminator (of the first, declared string), adds new one at the end.