c_str Method in C++

The c_str() built-in method has been widely used in C++ to return the character pointer of any string variable. This c_str() function returns the character pointer to its original variable array, which may or may not contain a null-terminated sequence of characters. It is simply used to get the total characters of a string. Thus, in this guide, we discuss different ways to use the c_str() function in C++. Let’s have a fresh start.

Example 01:

Let’s start our first example with the simple creation of a new c++ file. This needs to be done using the “touch” instruction of the Ubuntu 20.04 system. After that, we will open this file in any one of the built-in editors of Ubuntu 20.04 to start making code. Therefore, we have utilized the built-in GNU Nano editor so far. Both the commands are listed in the photo.

As shown, the newly created file is now opened already in the “GNU Nano 4.8” editor. The code must be started from the header files of C++ that are necessary for the code to work accordingly. So, the main header files used here are “string” and “bits/stdc++.h” to get standard input-output and use strings in the code. The namespace must be utilized before the main method to use “cout” and “cin” clauses, i.e., “std”. The main() function has been implemented and started with the initialization of a string variable “v1” with a string value “AqsaYasin”.

The first cout clause displays the value of string v1 on the shell. The other one is used to exhibit the variable v1 with the c_str() method. This c_str() method is returning the character pointer of v1. Within the “if” statement, we have used two built-in functions, “size” and “strlen” to get the size of variable v1 and its character pointer found using the c_str() method. The size() function is getting the size of the original variable v1. The other method, strlen(), is getting the size of the character pointer of variable v1 using the function”c_str()” and then comparing the size of both. If the size of the original variable v1 and its character pointer is the same, it will display the message of the first cout statement; otherwise, the else part cout statement will get executed. The main method and program are completed here. Just save this newly created code to avoid any inconvenience using “Ctrl+S”. Return to the shell using “Ctrl+X”.

The code will be compiled using the c++ compiler named g++ in Ubuntu 20.04 and can be installed using the package “apt”. The compilation went smooth, and we have executed the code after that. The execution displayed the variable v1 value and its character pointer value. As both have got the same size, therefore it displays that the size of both is equal.

Example 02:

Let’s take another simple example of using c_str() function in c++ code. So, the program has been started using necessary header files, i.e., “string” and “bits/stdc++.h”. The “std” namespace has been utilized here using the word “using”. Within the main() function implementation, we have initialized a new string variable v1 with the value “AqsaYasin”. The simple “for” loop has been initialized here to iterate the variable v1 characters. The loop will be iterated up to the size of a variable v1 that has been got using the built-in function “length()” in the loop. This “for” loop iterates the cout statement until the last character of variable v1 to display each character separately. Every character has been splitted from the string using the c_str() function on the variable v1 according to that particular iteration. This means, all the characters in a variable string v1 will be displayed on the shell separately at each line.

The compilation is essential when you will execute some c++ code so that the changes can be applied as well. The code will be executed with the “./a.out” command shown below. The output is clearly showing us the split of string v1 into separate characters at each line according to each iteration number, i.e., 1 to 9. This is how c_str() works to show each character.

Example 03:

Let’s just have a look at another simple example to elaborate on the working of the c_str() function of c++. This time we will be using it to split the string but with a split point mentioned in the code as well. This will be a little different as compared to the above example. So, we have initialized three of the main header files, i.e., “iostream”, “string”, and “cstring”. The “std” namespace is used after that.

The main() function gets started with the initialization of a string variable v1. You can see that this string value v1 contains some spaces between its words. These words and spaces combinely formed 1 string. Then we have declared a character type pointer “ptr” that has been getting the length of the variable string v1 using the built-in function “c_str()” on v1. The calculated variable length with added 1 in it has been used to create a memory for the pointer “ptr”. On the next line, we have utilized the “strcpy()” function to copy the character pointer of v1 found out by the “c_str()” function into the pointer ptr of the same size. As the “ptr” is a pointer, it will save the character pointer that is being found out using the c_str() method on the string v1.

Another pointer of character type “p” has been initialized. The strtok() method is used to split the pointer “ptr” from wherever it has space in it. The strtok() method tells where the space is and thus splits the string with the help of a while loop. Until the while loop is checked, the pointer “p” is not equal to 0 means having space; it will continue to split the string from the space point. This will be continued until the end of a string. In the end, the “ptr” pointer has been deleted, and the code is completed. Save this code with “Ctrl+S” and quit the editor with the “Ctrl+X” key shortcut.

After compiling and running this new file, we have got the splitted string with the c_str() method.

Example 04:

This program shows the simple c_str() function to get the equality of a string concerning its character pointer. The function “find_first_of()” is used to find the first occurrence or index of the ‘g’ character in variable v1, save to p1, and display it. Firstly, the first occurrence of the ‘g’ character from the character pointer of v1 has been found out using the c_str() function, and the “strchr” method is applied to convert ‘g’ to sring character. Then, it is subtracted from the original character pointer of v1. If both the p1 and p2 matched, it will show that both are equal.

After running this piece of code, we have been displayed with v1 and its character pointer. Also, the success message has been displayed.


This guide contains the explanation of c_str() function in C++. The examples mentioned above are easy to do and implemented on Ubuntu 20.04. We hope these will work on all Linux distributions. We have used the “for” and “while” loops in the code. We hope it works.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.