This article gives you an in-depth look at the C++ core string and the different ways of using it.
An in-depth Look into C++ Core String
Introduction
C++ does not have a core object type for string. The core string is derived by the coder. C++ has however, a string class in what is called, the standard template library. Using this string class is optional. The string class and features of the standard template library use the C++ core string in deferent ways. This means you have to understand the fundamentals of the core string and the different ways of using it. This article gives you an in-depth look at the C++ core string and the different ways of using it.
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).
How is a String demarcated in Memory?
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, \0. 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) = '\0';
--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. '\0' 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 '\0'. 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 \0. That is what the last but one statement in the above code does. Try the code.
Array and Core String
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. However, you can define a core string using an 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 \0. 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 \0. The following code illustrates this:
#include <iostream>
using namespace std;
int main()
{
char str[] = {'a', ' ', 's', 't', 'r', 'i', 'n', 'g', '\0'};
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 \0.
A String of Constant Content
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 (whether empty or not) returns a pointer to the first character of a string whose content is constant. The following code illustrates this:
#include <iostream>
using namespace std;
int main()
{
const char *str = "a string";
cout << str;
return 0;
}
You must assign the return value of the double quotes to a const char*. const char* means that the content of the object pointed to by the pointer, is constant. In the code, the content is "a string". In memory, the space is a one byte code. Also, in memory, the series of characters forming the string ends with \0 (which would not be displayed). Now that the content is constant, you cannot change in memory, any of the characters that is in the double quotes.
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 second and third cases above, the pointer points to the first char during creation.
With an array, the pointer is constant, but the characters of the string (array) can be changed. With a string literal, the content is constant, meaning the characters in the string cannot be changed, 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).
We have reached the end of the article. I hope you appreciate it.
Chrys
Written by Chrys
In C++ an array is a set of consecutive objects of the same type, in memory. We see how to crea...
A database is a set of related tables. This is part 1, division 1 of a series I have on databas...
To easy way lean java programming language with a basic concepts which will help to build basic...
its an tutorial to make a virus /worm in C language...
cracking unix system password all about unix cracking it quite easily...
A database is a set of related tables. This is part 1, division 1 of a series I have on databas...
In C++ an array is a set of consecutive objects of the same type, in memory. We see how to crea...
C++ is a computer language I want to teach in these tutorials. C++ is a very developed language...
In this article I give you reasons why website hosting has become cheap....
In this article I explain different free software types....