Core String in C++
C++ Taking the Bull by the Horns – Part 13
Forward: In this part of the series, we see how a string can be stored in memory and retrieved from memory.
By: Chrysanthus Date Published: 22 Aug 2012
Introduction
As I said, I present C++ to you in this series the way the inventors see it. I do the presentation in simple terms. I believe that in this way you would understand C++ better. Remember, take things in this series as I give you. Do not try to add or subtract any idea in your mind to or from what I give you; that would be misleading. You can do any subtraction or addition after you complete the series.
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
No Object Type for String
C++ has object types for int, bool, float, char and void. C++ does not have any object type for strings. So a way had to be worked out to store and retrieve strings from memory.
Characters in an Array
An example of a string is “the man”. We have an object type for characters, which is char. To store a string in memory we need to store chars (characters) that represent the string as consecutive objects in memory. A good way to do this is to have the chars in an array. We know that elements (objects) of an array are stored consecutively. So this is the beginning of our solution. Consider the following string:
“the man”
This string can be stored in an array as follows:
char myStr[] = {'t','h','e',' ','m','a','n'};
When you initialize an array like this, all the objects of the array are stored in memory consecutively. Note that each character in the string is now an object of type, char, in the array. Also note that the space between the words “the” and “man” is also stored in the array in an object, as ‘ ’. Remember that in the initialization of an array, all the array elements are separated by commas.
#include <iostream>
using namespace std;
int main()
{
char myStr[] = {'t','h','e',' ','m','a','n'};
cout << myStr[0]; cout << myStr[1]; cout << myStr[2]; cout << myStr[3]; cout << myStr[4];
cout << myStr[5]; cout << myStr[6];
return 0;
}
The output should display “the man” in one line. Read and try the code.
That is not a convenient way of handling strings. We have handled a phrase (string) character by character. That is not good; we should have a way of handling or referring to a phrase (string) using one identifier and not many identifiers (the array elements) as in the above case. To achieve this, the inventors of C++ decided that at the end of the array, you add the null character, , then C++ should consider the set of characters in the array as a string and one identifier can be used to identify (refer or handle) the string. The null character begins with a back slash, followed by zero, that is . The identifier that identifiers the resulting array is the identifier for the string. Read and try the following code that illustrates this:
#include <iostream>
using namespace std;
int main()
{
char myStr[] = {'t','h','e',' ','m','a','n',' '};
cout << myStr;
return 0;
}
Note that the identifier, myStr for the cout object is not in quotes and it does not have the square brackets. In the code, the last element in the array is the null character. It is in single quotes like the rest of the characters. Now, the array name which was supposed to be the identifier of a constant pointer to the first element (object) of the array and should return the address of the first element (object) of the array, now returns the string (characters of the array), when placed in a particular context, because the array is made of chars and it ends with ‘ ’. All the characters of the array are returned except the null character.
Normally, a pointer should not return any value of the pointed object or pointed objects. In the above code, the cout predefined object (context) has been design in such a way that if it receives a pointer to an array of chars ending with, , it should return all the characters in the array except the ending . Such a pointer still points to the first element of the array, but a context (cout object) can use it to obtain all the characters in the array.
We carry on.
replace the char array block that ends with the null character with a string in double quotes.
The typed string in double quotes returns a pointer (memory address) to the first element of the replaced array.
This pointer is the string pointer and a context (e.g. cout Object) can use it to return a string.
The returned pointer from the double quotes, can be used in the initialization of a char pointer to create a string.
The following code illustrates these:
#include <iostream>
using namespace std;
int main()
{
char *herStr = "the woman";
cout << herStr;
return 0;
}
Read and try the above code if you have not already done so (you may receive a warning message but just ignore it for now).
Note that in the initialization of pointers of other object types, the right operand returns the address (e.g. &myInt) of some object. A similar thing has happened here. An address is a pointer.
You can split the initialization into definition and assignment as in the following code (if you try the code, you may receive a warning message – just ignore that for now):
#include <iostream>
using namespace std;
int main()
{
char *herStr;
herStr = "the woman";
cout << herStr;
return 0;
}
At this point you may be wondering how to use the predefined cout object and its associated operands; do not worry, we shall study the cout object later.
String Literal
A string in double quotes is called a string literal.
Strictly speaking, there is no string pointer, as the pointer in question is pointing to the first character in the string. However, if you initialize (or define and assign) a string as in the above situations, then you can say you have a string pointer. You can increment or decrement the pointer (address in the pointer object) to point to the next or previous character (object) in the string, since the string is stored like an array (ending with ) in memory. Read and try the following code, which illustrates this (you may see a warning message – just ignore that for now):
#include <iostream>
using namespace std;
int main()
{
char *herStr = "the woman";
cout << *herStr; cout << "\n";
++herStr;
cout << *herStr; cout << "\n";
++herStr;
cout << *herStr; cout << "\n";
return 0;
}
To get the value (char) pointed to by the string pointer, the dereference operator (*) is used.
Constant Pointer to a String
A constant pointer to a string points to the first character of the string and the pointer (address in the pointer object) cannot be change. This means the address cannot be incremented or decremented. The following two statements show how you can create a constant pointer to a string:
char myStr[] = {'t','h','e',' ','m','a','n',' '};
char *const myStr = "the man";
Either of these constant pointers would return the whole string, in a particular context (cout object), but you cannot increment or decrement it. If in the second case, you have just a character pointer instead of a constant character pointer, then you would be able to increment or decrement the pointer.
Coding Very Long Strings
It is possible to have a string that is very long and coding it will mean it has to take more than one line. You will code it as illustrated in the following example. Read and try it (for now, ignore any error message displayed).
#include <iostream>
using namespace std;
int main()
{
char *longStr = "This is a very long string "
"that takes more than one line "
"to type in the source code.";
cout << longStr;
return 0;
}
Each part of the string that is in a line is in double quotes. Only the last part of the string is followed by the semicolon. The parts of the whole string before the last part are not followed by semicolon.
You can type the different parts of the string without pressing the Enter key as the following code illustrates (you can try the code):
#include <iostream>
using namespace std;
int main()
{
char *longStr = "This is a very long string " "that takes more than one line " "to type in the source code.";
cout << longStr;
return 0;
}
A string is a character array ending with the null character. A string needs a pointer to point to its first element. When a string is created by actually putting characters and the null character into an array, the pointer is a constant pointer. When it is created by assigning a string literal (double quoted text) to a pointer, during initialization or after definition, you have the option of making the pointer constant or not.
Strings as Array Elements
A string itself is an array, but can it be an element of an ordinary array? Yes, but you need to learn how to make a string an element of the array. This is because the string pointer has peculiar behaviors. Before we continue, remember, that when talking about a pointer, there are normally two objects concerned; the pointer object and the pointed object. In the case of strings, the pointed object is an array of chars (which is a derived object type).
We saw an array of pointers in the previous part of the series. However, it was an array of pointers to floats not an array of pointers to strings (arrays). You cannot have string literals as array elements. This is because a string literal (or char array ending with ) returns a pointer to the first character of its text and not the set of characters. So if you somehow want strings as elements of an array, you need to have the pointers to the first characters of the strings as the elements of the array. You would then have an array whose type would be pointer to chars. Something like,
char *arr[];
The array name (identifier) must be preceded by *. We had a similar definition in the previous part of the series, but instead of char, we had float. The following code samples show you how to use strings as array elements. Read and try them (ignore any error messages for now).
#include <iostream>
using namespace std;
int main()
{
char *one = "the first";
char *two = "the second";
char *three = "the third";
char *myStrings[] = {one, two, three}; //the block has pointers
cout << myStrings[0]; cout << "\n";
cout << myStrings[1]; cout << "\n";
cout << myStrings[2]; cout << "\n";
return 0;
}
The second code sample follows:
#include <iostream>
using namespace std;
int main()
{
char *myStrings[3];
myStrings[0] = "the first";
myStrings[1] = "the second";
myStrings[2] = "the third";
cout << myStrings[0]; cout << "\n";
cout << myStrings[1]; cout << "\n";
cout << myStrings[2]; cout << "\n";
return 0;
}
When you want an array of strings, the array has to be defined as the char pointer type. This array will ultimately have pointers to chars as elements (values). To assign a literal string to an element, just assign the char pointer of the string to the array element in the ordinary way, as in,
myStrings[2] = "the third";
In the above line, the double quotes returns a pointer, so a pointer is assigned as the element (value) for the index of the array. Since the array has been defined to have pointers, typing the array name and an index in brackets, would return a pointer. Now the context (cout Object) may obtain the value of the pointed object from the pointer and send. This is what the cout object does. It gets the char pointer (string pointer) as argument, obtains the value of the object pointed to by the char pointer and sends (returns) the value.
Before we leave this section, note that an array of pointers to char, can have pointers as its elements, as in the last-but-one (second-to-the-last) code sample above. This means, you can also get a string pointer from somewhere and assign it as an array element.
Also note: There is what is called the C++ String Library. After completing this series you should learn it. With its features you will be able to do a lot with strings.
Let us take a break here and continue in the next part of the series.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT