In this video, we'll learn about c-style strings.
C-style strings are about as simple as it gets.
They're a sequence of characters, stored contiguously in memory.
They're implemented as an array of characters, so you can access
individual characters using the array subscript syntax
that we've already learned.
How do you know where the sequence of characters ends.
C-style strings use a sentinel valley that marks the end of the string.
The null character is used, which is equivalent to the integer zero.
So c-style strings are often referred to as zero or null terminated strings.
We've been using c-style strings all along in this course.
We've used them mainly in our output statements as string literals.
Recall that string literals are sequences of characters
enclosed in double quotes.
These string literals are constants, so we can't change them just like
we can't change integer literals.
Even though we don't explicitly provide a null character at
the end of the string literal, c++ inserts one for us.
That's the way the end of string is handled.
Let's see an example of a string literal and how it's stored in memory.
Here's a real simple example that shows how c-style
strings are stored in memory.
In this case, we have a c-style string literal containing the
sequence of characters c++ is fun.
These characters are stored in a contiguous block of memory that
can be accessed as an array.
Notice that c++ is fun has exactly 10 characters, but the compiler
allocated 11 characters for the array because it needed space for
the null character at the end.
Literals are great and very useful, but we more often than not
need to create string variables.
Let's see how we can create c-style string variables.
In this example, we're declaring an array and initializing it
to Frank, using initializer list syntax as we've been doing
with integers and other types.
Notice that the c++ compiler will allocate six characters for this
array since it needs to terminate the string with the null character.
Also notice that the size of the array is fixed.
So if I wanted to add a y to Frank and create Franky, I couldn't
without having some potential problems since the new string
would not be null terminated.
You won't get a compiler error or even a warning.
In fact, in this case, my name sub-5 is still within the array bounds.
Let's see another example.
In this example, we are explicitly asking the compiler to allocate eight
characters for the character array.
And we're initializing it to Frank.
In this case, it will initialize the array with f r a n k,
and the rest of the space will be zeroed out with null characters.
In this case, if I wanted to change the character at index 5, which
is that first null character to a lowercase y, it would be just
fine since the string now contains Franky and is still null terminated.
Let's see another example.
In this example, we ask the compiler to allocate an array
of eight characters, and we don't initialize them.
This could be very problematic because if you use this array as a
string, all c-style string functions expect to find a null character.
And here there may or may not be one.
We really don't know what the data in the array is.
Suppose we wanted to display the string now.
How do you think this would happen, how do you think
c-style strings are displayed.
Well, in this case we start at the first element of the array
and we iterate through the array.
At every character, we see if it's the null character.
If it is, we stop since we reach the end of the string.
If it's not, we print the character and then we move
to the next character.
In this case, we don't know what's going to happen.
We might see a null right away or we might not see a
null for a very long time.
In the meantime, I'm printing stuff to the screen.
Some of the stuff may not even be displayable since some
characters are control characters.
You might think we can initialize c-style strings using the assignment
operator, just like we have with other types, but this won't work.
If we try to assign the c-style literal Frank to my name, we get
a compiler error because that's not the way c-style strings work.
Array names and literals evaluate to their location in memory, so
we're effectively assigning one location to another which is illegal.
Think of this as saying assign 10 to 12.
It doesn't make sense and the compiler won't allow it.
In order to assign one string to another, we need to use a
function called string copy.
That copies the c-style literal f r a n k to the my name array.
There are lots of c-style functions that c++ plus brings in
from the c programming language.
Let's see a few of them.
The cstring library contains many functions that work
with c-style strings.
These functions are used to copy, concatenate, compare, search, get
the length of a string and many more.
They all rely on one common factor that the sequence of
characters they're working on is terminated with a null character.
If you're using c-style strings, you must be very aware of this.
For example, suppose you have a c-style string and you
want to determine its length.
Well, let's say we have Frank.
Since no length information is stored with the string only the
characters in the array, the only way to determine the length is to
start counting at the first array element and then increment the longest
until you see the null character.
So in the case of Frank, we would look at the f, increment
one, look at the r, two, the a, three the, n four, the k five.
Then we see a null character, and we stop.
Now we know that the length is five.
But if that null character is not there, we keep
going and we keep counting.
So the result will very likely be incorrect, even worse if you're
copying one string into another and the string you're copying isn't null
terminated, you'll very likely exceed the bounds of the target string and
very likely cause a program crash.
This all sounds pretty ominous.
But in fact, we've been programming in c and c++ plus using c-style
strings for a very long time, and we've gotten pretty good
at understanding these issues.
But wouldn't it be great if we didn't have to worry
about these issues at all.
That's where c++ strings come in.
We'll talk about them later in this section.
Here a few examples that use the c-style string functions.
You typically have a function, and you pass into the function the name of the
string or the string literal or both.
We'll talk about functions in detail shortly.
But conceptually, they're really easy to understand.
For example, If I'm a function and my job is to return the color of a
crayon passed to me, then all i need from you is the crayon.
So you call me and send me the crayon, and I'll return
red, green, blue and so forth.
That's the idea, and here it's the same.
If you want to know the length of the string, pass in the string
to the strlen function, and it returns the string's length.
Here are just a few examples that copy, concatenate and compare strings.
C++ also has another library called c standard lib that contains functions
that convert strings to other types.
For example, if I have a string with the digits 1 and 2, I can
convert that to the integer 12.
Sometimes these are really useful functions.
However, all of these functions rely on the same thing.
They're all working with null terminated strings.
If you're using c-style strings, you have to be very aware of that.
Well that completes this video.
In the next video, we'll head over to the IDE, and we'll write some
examples using c-style strings.