0

This is the code I am seeking to do

std::string json_str;
const char json[] = json_str;

this is my try

const char json [json_str.size()] = {(char) json_str.c_str ()};

But it gives me the error "cast from 'const char*' to 'char' loses precision"

Please help. Thank you.

xinthose
  • 1,890
  • 1
  • 26
  • 46

2 Answers2

2
#include <string>

int main() {
    std::string json_str;
    const char *json = json_str.c_str();
    return 0;
}
alifirat
  • 2,659
  • 1
  • 11
  • 30
1

Possible solutions that come to mind:

std::string json_str;
const char* json = json_str.c_str();

You can use json as long as json_str is alive.

std::string json_str;
const char* json = strdup(json_str.c_str());

You can use json even after json_str is not alive but you have to make sure that you deallocate the memory.

R Sahu
  • 196,807
  • 13
  • 136
  • 247