Strings are objects that represent sequences of alphanumeric characters. The standard string class provides support for such objects with an interface similar to that of an array of bytes. Strings specifically designed to operate with strings of single-byte characters.
In C programming language ASCII codes are used as in char arrays to store texts in ASCII mode. You can use char arrays in a C or C++ app. They are faster in operations, and they have less memory usage. Strings are useful for storing text and they are defined in the string library. A string
class contains a collection of characters surrounded by double quotes as we used in char arrays.
Printing a Pointer of a Char Array in a C++ app
Before the string, let’s see how pointer of a character array strings can be used and printed,
1 2 3 4 5 6 |
const char c_str[] = "This is a test char array"; const char *cstr; cstr = c_str; // This is a correct way to print pointer of an char array std::cout << cstr << '\n'; |
We can test other methods to print, all others will print the address of the pointer, see example below;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <string> int main (void) { const char c_str[] = "This is a test char array"; const char *cstr; cstr = c_str; // This is a correct way to print pointer of an char array std::cout << cstr << '\n'; // These results the address of pointer std::cout << (void *) cstr << '\n'; std::cout << (const void *) cstr << '\n'; std::cout << static_cast<const void *> (cstr) << '\n'; std::cout << static_cast<void*>(&cstr)<< '\n'; getchar(); return 0; } |
In this example only the first std::cout
usage is correct, others will print out the address of the cstr, not the text content, output will be as follows,
1 2 3 4 5 6 7 8 9 10 11 |
This is a test char array 004290D4 004290D4 004290D4 0019FEF0 |
Printing a String Pointer
A string variable can be used for a static string, or it can be used as a pointer. If we want to use a string pointer and we want to print out this string, we should use
1 |
static_cast<const std::string >( *str) |
where str
is a pointer of a string. Here is an example,
1 2 3 4 5 6 |
std::string s = "This is a test string"; std::string *str ; str = &s; // this is the correct way to print out texts of pointer string std::cout << static_cast<const std::string >( *str) << '\n'; |
Here is a full example, including wrong usages,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <string> int main (void) { std::string s = "This is a test string"; std::string *str ; str = &s; // this is the correct way to print out texts of pointer string std::cout << static_cast<const std::string >( *str) << '\n'; // these below will result with address of the pointer std::cout << str << '\n'; std::cout << (void *) str << '\n'; std::cout << (const void *) str << '\n'; std::cout << static_cast<const void *> (str) << '\n'; std::cout << static_cast<void*>(str)<< '\n'; getchar(); return 0; } |
and the output will be,
1 2 3 4 5 6 7 8 9 10 11 12 |
This is a test string 0019FE94 0019FE94 0019FE94 0019FE94 0019FE94 |
As you see if we have pointer string only the first std::cout
is correct, others will printout the address of the pointer as in this example.
Download a free trial of C++ Builder and try these examples today!
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition