The safest way is to copy the line, then call the C function:
void getAgeSafe(const char* name) { std::vector<char> tmp = name? std::vector<char>(name, name+1+strlen(name)) :std::vector<char>(); getAge( tmp.data() ); }
and call getAgeSafe from your C ++ code.
A less secure way, which relies on C code that never changes the char* name , is const_cast , again in the "wrapping" function:
void getAgeUnsafe(const char* name) { getAge( const_cast<char*>(name) ); }
but this time the name is more terrible, like the operation. If you call getAge with a constant compile-time constant, for example, "bob" , if getAge changes its input, the results are undefined (this is true in both C and C ++ - C ++ at least warns you about this )
source share