Tip:
Highlight text to annotate it
X
As part of the compiler there are built in functions that you can use with null-terminated character arrays or NTCAs,
otherwise known as c-strings, or c-style strings. Most of these functions you can
you know, write yourself. It'd be just as easy to do so, but they're there for your convenience. In fact there are
alternate versions that I'll mention at the end of this lesson.
So, let's take a look first at the string length function. String length will return an integer and
take in a null-terminated character array and what it returns is the length of the data in that array. So for example,
if I declare a character array and initialize it to "hello" then see a picture of it here,
and the data is of length what?
It's of length five. That is there are five characters before
the null-character. Passing "array" to "strlen" will return five and our output then will be 5. Ugh... feeling sick...
Let's take a look at string copy. String copy will return nothing,
but it takes as arguments two null-terminated character arrays.
If I have "source" length 10 and it's initialized to "Hey" and "target" again of length 10 and it's initialized to "Boy Howdy".
Okay, here are pictures of these two arrays, and if I do a string copy,
"target, source", what this is going to do will copy the value of the
c-string or null-terminated character array, of "source" into "target". Passing and then you see that
"target" no longer reads "Boy Howdy", but "Hey" and then the null-character
and "Howdy". The null-character is automatically placed in the array by the built in function.
So, for example, we want to give a value to a null- terminated character array after it has been declared.
Well, now you've got the answer to this question. Suppose that I declare a character array of length say 10.
Okay, so this entity has been declared.
How do I give it a value of say, "Bob"? Can I do this? No, absolutely not. That will not compile.
Remember we learned that in the last lesson. You cannot assign to null-terminated character array. So, how do we do it?
Simply use string copy. "strcpy",
the target, which in this case would be "array", and then what it is you want to copy into it. That would be the string
"Bob". So this should be an indication as to how a constant literal is stored internally
in the compiler. It's stored as a null-terminated character array. Because the
arguments to string copy must be null-terminated character arrays.
What if we have "strcpy(target, "Hi"); Okay, so "target" is turned into "Hi" and then
the null character. Now remember that whatever comes after that null character,
is no longer data, it's just junk. What happens then if I do a string copy to "target" of the string "BurritoToppings"?
What I'm going to get is "BurritioTop" and I've run out of room. This is one of the failings of the built in functions for c-strings.
That is that if the data fits, the null character is automatically placed at the end of the data.
But, if the data doesn't fit then you've got a real problem.
Okay, so there really should be a null character here,
but there is none. And now what you have is with an array of characters that
would be thought of as being a null-terminated character array, but it is not. The null character is not placed in there and this can cause some real
problems in the future. So you have to be really, really careful about having the data fit into the structure.
Okay, moving right along, string concatenation is taken care of by the "strcat" function.
Again, this function will take as arguments two null-terminated character arrays and it returns nothing. "source" is an array of 10,
And let's give it an initial value of "There". "target", again an array of 10, let's give it an initial value of "Hi". What happens when I
string cat "target" and "source"? Well, "source" is going to be appended to the end of "target". It's concatenated.
I'm going to end up with a "T-H-E-R-E" here and then the null character and sure enough there it is. Again, the chance that
you're going overshoot the length of this array. So, what happens here? "strcat(target, "!!");
That's going to put an exclamation here and here and a null here.
and we'll see, sure enough. What happens if I do it again then? That's going to put an exclamation point here and no null character
and we end up with what we thought should be a null-terminated character array, but it isn't.
Moving right along to "strcmp". Now, you remember that it was illegal to do this,
to say "if(ntca1 == ncta2)" then
so forth and so on, we can't do this, this is illegal. So, how is it that we compare two strings,
two null-terminated character arrays?
We do it with the string compare function.
Now, here is an interesting fact. This function is going to return an integer and one might think, well you know, we can
return zero if they're not the same, they're not identically the same, and we can return one if they are the same.
That would seem logical, except that I can return more information
if I change up how I do this. And different implementations of the function will do different things, but our implementation is as follows.
It will return to zero if the two strings are identical and it'll return 1 or -1 depending on
which one is "larger" than the other one. The comparison is made lexicographically.
That is, the compiler will run down the array, looking at each character, comparing the ASCII values.
And when one position differs then the comparison stops and one is gonna be returned if the ASCII value of the present
character is, in "ntca1", is larger than theASCII value of the current positioned character in "ntca2".
Okay, so let's take a look at an example here. We have "array1" is "bob", "array2" is "bob" and "array3" is "Bob", but with an uppercase 'B'.
So let's string compare, passing "ncta1" and "ntca2". Well they're identical, so this is going to return zero.
How about to string comparing "ntca1" and "ntca3"? Lower case "bob", compared to
uppercase 'B', lower case 'ob'. When the compiler gets to that first character it notices a difference.
Now, the ASCII value for the lowercase 'b' is 98, if I remember correctly. And for the uppercase 'B', I believe that's 66.
In any case, lower case 'b' is greater than the uppercase 'B' and so this going to return a 1.
Okay, how about comparing "ntca3" to "ntca1"?
That's going to give us -1 because of course it's exactly opposite.
And then comparing "ntca1" to the constant literal string, "bobby", well
we get the same at the first location,
second location, third location. But, now for "ntca1", the next character is the null character, its value is zero and that's compared to
lower case 'b' which is 98. And so this is gonna give you a -1. That is the second argument
is greater than the second string argument to the function, has a greater value in that position than corresponding location in the first
array. Let's take a look at an if-statement. If not string compare "ntca1", "ntca2", these strings are identical. So, I'm asking
if the two strings are identical, output this message. So why the not? Well, again remember that if
the two strings are identical then it's going to return a zero which is synonymous with false, and so it makes sense that
you put that not there. The dangers, of course, always you can walk off the array. With both string copy
and string cat. So, for example, if "source" is "Walk This Way", "target" is empty, let's do a string copy, of "source" into "target".
So, now I've got "target" nearly full and then I'm going to string cat "source" onto "target" again.
And I end up with a string that's far too long to put into that array. We also have available three other functions, another version of the very
same function. Notice the difference is the 'n' in the middle. And the only difference here is that now
these functions have a third parameter so they take a third argument and that is for string copy, the third argument is going to designate
how many characters you want a copy, for string compare how many
characters you wanna compare, and for string cat how many characters of the source do you want to concatenate
over onto the target. Now that problem with these functions that is
they are thought of as being safer, and they're not. You can't walk off the array just as easily with these
as you can with the others. I don't think I've ever used these.
But, of course, you can.