krish
krish

Reputation: 95

Any possible issues if const char * is converted to char * in a function call

In fun1() I am getting a string and I need to pass this string data to a function fun2() which takes argument char *

function2 prototype is as below

void fun2(char *p);

I am calling fun2 from fun1 as below

void fun1()
{
    fun2((char *)str.c_str());
}

Is there any chance from the conversion from const char * to char * does make any potential issues

Upvotes: 1

Views: 72

Answers (1)

flogram_dev
flogram_dev

Reputation: 42858

If fun2 tries to modify the data pointed to by the const char* returned by std::string::c_str then it's undefined behavior.

If fun2 doesn't modify the data pointed to by p, then there won't be any problem. (But then fun2 should be declared to take a const char*, not a char*, anyway.)

Upvotes: 7

Related Questions