Lecture 5: More C Strings
We will look at the underlying memory representation of C strings.
Video 1: String Searching and Spans
We will learn about a few more built-in string functions.
strchr
: returns a pointer to the first occurrence (or null) of the specified character in a string. Usestrrchr
for last occurrence.
strstr
: returns a pointer to the first occurrence of the second string in the first, or NULL if it cannot be found.
We will now introduce "span functions" which are a little funky but will be useful down the road.
Video 4: Pointers
- Pointers are variables that hold an address to some memory on the heap.
- In C++, we had pass by reference. Unfortunately, in C, there is only pass by value. So instead, we use pointers to ensure changes persist and that we are not duplicating memory. Actually, C does this implicitly for us already for arrays (and therefore strings since strings are arrays)! All other data types (such as ints) are passed by value and not converted automatically to pointers so you'd actually be passing in a copy of the value. You can get around this and implicitly pass by "reference" by sending in a copy of the pointer address itself using
&variable
).
Video 5, 6, 7: Strings in Memory
- 7 key behaviors: