Core String in C++

C++ Taking the Bull by the Horns - Part 13

Chrys Forcha
Introduction
This is part 13 of my series, C++ Taking the Bull by the Horns. A string is a human language word, phrase or sentence. In this part of the series, we see how a string can be stored in memory and retrieved from memory. Before we continue, remember that an object is a region in memory.

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 store 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.

In order for us to print (retrieve) the elements in the array so that they appear as a string that you would type (characters in a group), we would have to print the characters one by one without printing the newline (\n) character that would cause characters to be printed on new (different) lines. The following code illustrates this:

#include
using namespace std;

int main()
{
char myStr[] = {'t','h','e',' ','m','a','n'};

cout This 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, \0, 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 \0. The identifier that identifiers the resulting array is the identifier for the string. Read and try the following code that illustrates this:

#include
using namespace std;

int main()
{
char myStr[] = {'t','h','e',' ','m','a','n','\0'};

cout
return 0;
}

Note that the identifier, myStr for the cout object is not in quotes. 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 '\0'. 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, \0 it should return all the characters in the array except the ending \0. 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.

Still, coding a string by filling an array with elements and ending it with, '\0' is not convenient for the programmer. So the Inventors of C++ decided to
1) replace the char array block that ends with the null character with a string in double quotes.
2) The typed string in double quotes returns a pointer (memory address) to the first element of the replaced array.
3) This pointer is the string pointer and a context (e.g. cout Object) can use it to return a string.
4) 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
using namespace std;

int main()
{
char *herStr = "the woman";

cout
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 declaration and assignment as in the following code (if you try the code, you may receive an warning message - just ignore that for now):

#include
using namespace std;

int main()
{
char *herStr;
herStr = "the woman";

cout
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 in the series.

String Literal
A string in double quotes is called a string literal.

Incrementing and Decrementing String Pointer
Strictly speaking, there is no string pointer. However, if you initialize (or declare 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 \0) in memory. Read and try the following code, which illustrates this (you may see a warning message - just ignore that for now):

#include
using namespace std;

int main()
{
char *herStr = "the woman";

cout ++herStr;
cout ++herStr;
cout 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','\0'};

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
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
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.

A String
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 declaration, 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 \0) 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 declaration 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
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
The second code sample follows:

#include
using namespace std;

int main()
{
char *myStrings[3];

myStrings[0] = "the first";
myStrings[1] = "the second";
myStrings[2] = "the third";

cout
When you want an array of strings, the array has to be declared as the char pointer type. This array will ultimately have pointers to chars as elements (values). To assign a string to an element, just assign the char pointer of the string to the element array 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 declared 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.

Note: 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

To arrive at any of the parts of this series, just type the corresponding title below in the Search Box of this page and click Search (you can also use any available links):

C++ Taking the Bull by the Horns - Part 1
C++ Taking the Bull by the Horns - Part 2
C++ Taking the Bull by the Horns - Part 3
C++ Taking the Bull by the Horns - Part 4
C++ Taking the Bull by the Horns - Part 5
C++ Taking the Bull by the Horns - Part 6
C++ Taking the Bull by the Horns - Part 7
C++ Taking the Bull by the Horns - Part 8
C++ Taking the Bull by the Horns - Part 9
C++ Taking the Bull by the Horns - Part 10
C++ Taking the Bull by the Horns - Part 11
C++ Taking the Bull by the Horns - Part 12
C++ Taking the Bull by the Horns - Part 13
C++ Taking the Bull by the Horns - Part 14
C++ Taking the Bull by the Horns - Part 15
C++ Taking the Bull by the Horns - Part 16
C++ Taking the Bull by the Horns - Part 17
C++ Taking the Bull by the Horns - Part 18
C++ Taking the Bull by the Horns - Part 19
C++ Taking the Bull by the Horns - Part 20
C++ Taking the Bull by the Horns - Part 21
C++ Taking the Bull by the Horns - Part 22
C++ Taking the Bull by the Horns - Part 23
C++ Taking the Bull by the Horns - Part 24
C++ Taking the Bull by the Horns - Part 25

Published by Chrys Forcha

I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.