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
  • 3,213
  • 3
  • 40
  • 59

2 Answers2

2
#include <string>

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

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
  • 204,454
  • 14
  • 159
  • 270