An in-depth Look into C++ Core String
C++ Just After the Basics - Part 5
Forward: This article gives you an in-depth look at the C++ core string and the different ways of using it.
By: Chrysanthus Date Published: 22 Aug 2012
Introduction
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.
You need to have basic knowledge in C++ before reading this article. If you do not have that knowledge, then read the series I wrote whose first part is titled, “Getting Started with C++”. To reach the article, type the title and my name in the Search Box of this page and click Search.
Deriving the Core String
A string is a continuous series of char objects in memory. There is no gap in the series. By the word gap, I am referring to one or more consecutive memory cells that is (are) not associated with an object type. Note, a space typed by the spacebar key of the keyboard, does not create a gap in a string. It puts in a char with a char value. This is just like other char values, but instead of being displayed as a character on screen it is display as space. This means, so far as the computer is concerned, a space is a character (char).
The start of a string in memory is identified by a char*, which is a pointer to the first char of the string. The end of a string is the null character, . It is followed by zero (not letter O). This null character is part of the string but it is not normally displayed on screen or printed. If the pointer is not a constant pointer (constant memory address), then it can be incremented to point to the next character of the string. Let us now form a core string. The following code illustrates this:
#include <iostream>
using namespace std;
int main()
{
char *str;
*str = 's';
*(++str) = 't';
*(++str) = 'r';
*(++str) = 'i';
*(++str) = 'n';
*(++str) = 'g';
*(++str) = ' ';
--str;
--str;
--str;
--str;
--str;
--str;
cout << str;
return 0;
}
In the code, the pointer to a char is first declared. Next the value of this pointer is made ‘s’. The pointer is incremented a number of times, and to each increment, a char is assigned. ‘ ’ is assigned to the last increment. You now have a continuous series in memory with the word, “string”; at the end of this word, you have ‘ ’. After this the pointer is decremented to point to the first character, ‘s’, in the string.
The cout object from the iostream header has been designed in such a way that it would take the pointer to a char and display the characters beginning from that pointer until it sees . That is what the last but one statement in the above code does. Try the code.
An array can be used to define a core string. The name of an array is a constant pointer. Unlike the pointer we saw above, you cannot increment the array name because it is constant (pointer). However, you can define a core string using the array. We saw above that a string is a series of chars in consecutive memory cells and the last character (char) of these cells is . So to use an array to define a string, let the array be an array of chars; the last character of the array should be a . The following code illustrates this:
#include <iostream>
using namespace std;
int main()
{
char str[] = {'a', ' ', 's', 't', 'r', 'i', 'n', 'g', ' '};
cout << str;
return 0;
}
The value of the string is “a string”. Note that in order to have a space as a character I type, ‘ ’. Typed a space in single quotes. In memory that space will be represented by a piece of code of one byte length.
Note: The array name is a pointer (constant pointer) that points to the first element of the array (in this case, of chars). The cout object will send out all the characters beginning from the pointer, until it sees .
A String Literal
When you type a string in double quotes in C++, that (text in double quotes) is called a string literal. An empty string literal is the opening and closing double quotes just next to one another. The double quotes returns a pointer to the first character of the string. The following code illustrates this:
#include <iostream>
using namespace std;
int main()
{
const char *str = "a string";
cout << str;
return 0;
}
Well, the content is constant, but the pointer is not constant. You can actually increment the pointer. Constant pointer and constant content are not the same things. Note that in the above code, the pointer points to the first element of the string. In the following code, the pointer is incremented. An attempt is made to change the pointed new character and it is forbidden. Because of this refusal, the compiler issues an error message. Try the code below:
#include <iostream>
using namespace std;
int main()
{
const char *str = "a string";
++str;
*str = 'e';
cout << str;
return 0;
}
Constant Pointer and Constant Content
We have seen three situations in which a string can be created. With all strings, no matter the form, the pointer of the string should be made to point to the first char of the string. With the doubtful second and third code samples above, the pointer points to the first char during creation, even though in the third case, all the characters are in practice, constant.
When a string literal is assigned to a “const char *”, its content cannot be change. This is what happens in practice, but in theory it is not supposed to be so. In theory, it is the first character in the literal that should be constant.
Now, when you assign a string literal to an array of chars, it will be possible to change the characters (values) of the literal using the array name and its square brackets. The following code illustrates this:
#include <iostream>
using namespace std;
int main()
{
char str[] = "Children of Paridise";
cout << str <<'\n';
str[0] = 'P';
str[9] = 'i';
str[10] = 'n';
cout << str <<'\n';
return 0;
}
Read and try this code if you have not already done so.
With an array, the pointer is constant, but the characters of the string (array) can be changed. With a string literal, assigned to a “const char *”, the content is constant, meaning the characters in the string cannot be changed, using the pointer, but the pointer is not constant, and you can change the pointer. With the first code sample above, neither the pointer nor the string content is constant; either can be changed (modified). However, you can change the content of the string literal, if it is assigned to an array. The content of the string literal is considered constant only when you are dealing directly with a const char *. In practice, it is also advisable to use the string literal as a constant, unless you have a good reason not to do so. These are C++ nuances that you just have to learn.
If you really want to write a program that would be accessing and changing the characters of a string, then you would have to use the C++ predefined String class. I will address that in a different tutorial series.
In the following code, the parameter of the function, fn, is a “const char *”. The argument of the function call, is just the string literal in double quotes. That is how you use the string literal for parameter and argument of a function. Read and try the code.
#include <iostream>
using namespace std;
void fn(const char * str)
{
cout << str;
}
int main()
{
fn("This is a string literal.");
return 0;
}
As soon as the function begins execution, the following statement is made (executed) unknown to you:
const char * str = "This is a string literal.";
This is why it is possible to use the pointer, str, of the parameter, for the string literal in the function body.
We have reached the end of the article. I hope at this point, you are a master of the C++ core string. We 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