- [Instructor] In C++, the primitive string type
is an array of characters terminated with a no value.
This is sometimes referred to as a cstring
or a null terminated string,
to distinguish it from the object oriented string types.
Here's a working copy of c-strings.cpp
from chapter three of the exercise files.
And you noticed that I have a string here
and it's declared as an array of characters.
The character type is technically
an integer of a size suitable for holding a character,
which is eight bits on most common systems.
So a string is an array of characters
terminated with a zero value.
So this six character string takes up seven values
in the array.
So this is an array of characters.
I can display it with puts,
I can iterate through it with a for-loop
and print each character individually.
And so when I build and run this,
you see, I display the string
and then I display each character, zero through five,
six characters, one, two, three, four, five, six,
and I'm not displaying the null terminator.
So this is what a string is, it's an array,
but this line of code, what this does is,
of course it declares the array
and it copies the literal string into the array.
The literal string was not originally in the array.
It was separate as a literal string.
And in order to put it into the array,
it needs to be copied.
Now, most often you don't actually need to do it that way.
You don't need a copy of the string.
So what you'll see more often is this,
a character pointer, which is assigned to the address
of the literal string.
And you notice it needs to be const qualified
because the literal string is naturally immutable.
In fact, in modern versions of C++,
it wasn't always this way,
but in modern versions of C++
if I don't have this const qualifier and I try to build,
I get a warning that C++ 11
does not allow conversion from string literal
to character pointer.
And if I click on this,
well, it doesn't offer to fix it for me.
But the way to fix that
is to make this a const character pointer.
And so putting const in front of character pointer like this
actually makes the pointer a pointer to const care.
It doesn't make the pointer itself a constant.
It makes what it points to the type of the pointer
as const care.
So it's a pointer to constant characters.
If you actually wanted to have a constant pointer
that cannot be changed,
you put the const after the asterisk,
and of course that still works
and you just can't reassign the pointer to something else.
I can't come in afterwards and say,
cstring equals like no pointer or something.
If I try to build that, it says I can't
because it's a const pointer,
but I can, if I do this, of course,
it wouldn't work very well and it would crush.
So what we have here with the const character pointer,
this works exactly the same as it did with the array.
We can still index it as if it's an array.
We can treat it in every way, the same way,
the only difference is were no longer copying
the literal string into an array.
So we're saving some time, we're saving some code
and it's more efficient this way.
And this is the way that you'll normally see it.
One other thing I want to show you
is string concatenation in C++.
If I wanted to add another string on the end of this,
I could say another string,
you notice I have a space in there before it.
Concatenating two strings in C++
at least two literal strings
is just a matter of putting them one after the other,
more often than not you'll see this
when they're on separate lines, something like this.
And so when I build and run,
you notice that we now have 20 characters
and its a string and other string.
So cstring is a very simple form of strings,
much simpler than the string class provided by the STL,
for circumstances where you don't need the power
of object oriented strings,
cstrings are simple, small and fast,
and they're very common in C++
the STL string class will be covered later in this course.