C++C++11C++14C++17Introduction to C++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,

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

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,

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

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

Here is a full example, including wrong usages,

and the output will be,

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!

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is An Eligible Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What Is A Trivial Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What Is Uppercase T in C++ Syntax?

C++C++11C++14C++17Learn C++Syntax

What Is An Implicitly-defined Copy Assignment Operator In C++?