Site icon Learn C++

This Is How To Print A Pointer String In A C++ App

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,

[crayon-6637ba55734d3848571131/]

We can test other methods to print, all others will print the address of the pointer, see example below;

[crayon-6637ba55734da361172950/]

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,

[crayon-6637ba55734dc800689650/]

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

[crayon-6637ba55734df809832130/]

where str is a pointer of a string. Here is an example,

[crayon-6637ba55734e1587256843/]

Here is a full example, including wrong usages,

[crayon-6637ba55734e2843624575/]

and the output will be,

[crayon-6637ba55734e4633905946/]

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!

Exit mobile version