Convert string to char* in C++

In this article, we will discuss different ways to convert a string to char* in C++.

Table Of Contents

Method 1: Using string::c_str() function

In C++, the string class provides a member function c_str(). It returns a const char pointer to the null terminated contents of the string. We can call this function to get a const char to the characters in the string, and then cast it back to char using const_cast<char*>. Let’s see an example,

#include <iostream>
#include <string>

int main()
{
    std::string sampleText = "This is a sample text";

    // Convert string to char*
    char* textPtr = const_cast<char*>(sampleText.c_str());

    std::cout << textPtr << std::endl;

    return 0;
}

Output:

This is a sample text

It converted a string to a char pointer in C++.

Method 2: Using string::data() function

In C++, the string class provides a member function data(). It returns a const pointer, pointing to the internal contents os string. So, we can use this to get a const char to the characters in the string. Then cast it to char using const_char<char*>. Let’s see an example,

#include <iostream>
#include <string>

int main()
{
    std::string sampleText = "This is a sample text";

    // Convert string to char*
    char* textPtr = const_cast<char*>(sampleText.data());

    std::cout << textPtr << std::endl;

    return 0;
}

Output:

This is a sample text

It converted a string to a char pointer in C++.

Method 3: Using [] operator

If you are using the latest compiler of C++ i.e. C++11 onwards, then you can directly take the address of first character of string i.e. &strObj[0]. As, from C++11 onwards,
std::string does the contiguous memory allocation for characters. So, we can just fetch the address of first character, and assign it to a char pointer. Let’s see an example,

#include <iostream>
#include <string>

int main()
{
    std::string sampleText = "This is a sample text";

    // Convert string to char*
    char* textPtr = &sampleText[0];

    std::cout << textPtr << std::endl;

    return 0;
}

Output:

This is a sample text

It converted a string to a char pointer in C++.

Method 4: Using STL Algorithm copy()

Allocate a char array on heap, and assign it to char*. Length of array will be equivalent to the size of string. Then use the STL’s copy() algorithm to copy the characters of string to the char array. Let’s see an example,

#include <iostream>
#include <algorithm>
#include <string>

int main()
{
    std::string sampleText = "This is a sample text";

    // create an array on heap
    char* textArr = new char[sampleText.size() + 1];

    // Convert string to char*
    std::copy(sampleText.begin(), sampleText.end(), textArr);

    std::cout << textArr << std::endl;

    delete[] textArr;

    return 0;
}

Output:

This is a sample text

It converted a string to a char pointer in C++.

Summary

We learned about three different ways to convert a string into char* in C++. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top