5
string str1 = "hello";
    const char* string1 = str1; 

I get an error..

cannot convert ‘std::string {aka std::basic_string}’ to ‘const char*’ in initialization

how do i cast string to const char*

Thanks for helping

billz
  • 44,644
  • 9
  • 83
  • 100
user1777711
  • 1,604
  • 6
  • 22
  • 32

3 Answers3

18

how do i cast string to const char*?

use std::string::c_str() function, it returns a non-modifiable standard C character array version of the string.

const char* string1 = str1.c_str();
billz
  • 44,644
  • 9
  • 83
  • 100
2

Try const char* string1 = str1.c_str();

Constantinius
  • 34,183
  • 8
  • 77
  • 85
0

How about this solution:

string str1 = "hello";
const char* string1 = str1.c_str();
Alexander Mihailov
  • 1,154
  • 7
  • 15
Subhajit
  • 320
  • 1
  • 6