- [Bill] In C++, the primitive string type
is an array of characters terminated with a null value.
This is sometimes referred to as a C string
or a null-terminated string
to distinguish it from the object-oriented string types.
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 two const char.
It doesn't make the pointer itself a constant,
it makes what it points to
the type of the pointer as const char.
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,
C string equals like null 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 we're 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 wanna 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 it says string and other string.
So C strings are 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,
C strings are simple, small and fast
and they're very common in C++.
Some characters in C++ have special meaning
or otherwise not normally accessible
within the parameters of the C++ language.
Escape sequences are used to access these characters.
Each escape character begins with a backslash character.
And if you need to type the actual backslash character,
then you use a double backslash
to result in a single backslash.
So I'll go ahead and build and run this,
and you can see that it displays hello world.
And if I wanna add a single quote,
I use backslash single quote,
I use backslash double quote to get a double quote.
I use two backslashes to get a backslash or \x40.
I can access any character in the ASCII table
by giving its hexadecimal value beginning with an x.
And so this will be the @ sign.
And if I go ahead and build and run here, you see,
I get that single quote, double quote backslash,
and the @ sign.
If I wanna get a new line,
I can use backslash n, and if I wanna get a tab,
I can use backslash t.
And if I go ahead and build and run this,
you see we have a new line and a tab.
So world is starting in dented there on a line by itself.
You can get any character in the Unicode table
on a system that supports Unicode.
I can say u03bc, and this'll give me a Greek letter mew
on systems that support this.
So when I build and run,
you'll see the Greek letter mew there.
Notably Microsoft visual C++
does not support Unicode by default,
at least as of the time that I'm recording this.
So your results here may vary.
So this is a table of the most common
standard character escape sequences.
You can find a table like this online,
pretty much anywhere.
Character escape sequences are commonly used
to provide special characters in the string constants
that may not otherwise be accessible on the keyboard
or due to conflicts in the language.
The standard C++ library provides a number of functions
for working with C strings
and are found in the C string header.
This is where the string functions are declared.
We've also got some constants, a max buff size, 128,
a couple of string constant, string one and string two.
And then we have a couple of character buffers,
SD one and SD two inside of Maine,
along with an integer, a character and a character pointer,
which we will use for various purposes
in our demonstrations.
First up is strncpy.
And you'll notice that this has an n
in the middle of the name.
Many of these functions have this n
in the middle of their names,
this means that the function uses a count argument
to limit the number of characters
upon which it will operate.
There are often other versions of the same functions
that do not have the n in their names.
Try to favor the version with the n
as it is usually more stable and safe.
So strncpy is used to copy a string
from one place to another.
And here we're copying the constant string s one
into the buffer SD one,
and we're using max buff as the count.
It will copy up to max buff minus one characters,
and we do the same with SD two.
And if I build and run, we can follow along here.
And you see at the top of our results,
here is our print f's, SD is and SD two is
string one and string two.
Next is strncat for concatenating strings.
First we can catenate a space dash space string into SD one.
And then we can catenate SD two on top of that.
And each time we're using as our count,
the length of the max buff
minus the length of the string minus one.
And so we do this in both cases,
and that's the length of the destination string,
so that we know that there's enough room in there
for the concatenation.
And you notice our result here,
SD one is now string one dash string two
with the spaces around the dash.
String length, stir n length,
will get the length of a string.
And again, it uses the count max buff
so that it doesn't count past the end of a buffer.
Strnlen returns a size t value.
And so you'll notice in our print f
we're using the ZD for the value.
Stir CMP for comparing strings.
This returns a one
if the comparison is greater than a minus one,
if it's less than and a zero, if the strings are identical.
And so here we compare them
and you notice that we get a result of not equals
for SD one and SD two, and the value is minus five.
And we have a zero for comparing SD two to S two,
because they are identical.
Strchr is for finding a character in a string.
And here we set our character variable
to the letter n and we search for the n in SD one.
And did we find an n?
Yes.
And stir care returns the position
of the first matching character or null.
And there's also a version that searches from the right,
which is stir RCHR with two RS in the middle there.
And there's stir stir,
which searches for a string in a string.
And you notice down here, we're looking for S two in SD one.
And of course, yes, we find it,
and we find it at position 13.
And again, stir stir returns the position of the string
or null if it's not found.
Many systems have non-standard extensions
to this function,
so be sure to consult your system documentation.
Even if you're using C++
you should never be afraid to use C strings.
There are times when they're necessary
and there's times when they're just smaller and faster
than C++ STL strings.
These functions are a great resource
for working with C strings.
(upbeat music)