From what I read here, you can't do :
char *delegates[]={"IN",NULL};
it has to be this so you do not get a warning :
const char *delegates[]={"IN",NULL};
But I have some function that I can't change that looks like :
void Interpreter::setBuiltIns(char *builtins[],int num )
This function is not going to change the array in any way.
If I try to pass it the strings array with :
myclass.setBuiltIns(delegates, 1);
I get an error, but if I remove the const
from delegate
there is no error but I get the ISO warning
.
How can I keep this function, and pass it the array without a warning/error.
One effective way to convert a string object into a numeral int is to use the stoi() function. This method is commonly used for newer versions of C++, with is being introduced with C++11. It takes as input a string value and returns as output the integer version of it.
char* is typically used to iterate through a character array, i.e., a C string. It is rarely used as a pointer to a single char, unlike how other pointers are typically used. C++ has newer constructs for strings that typically should be used.
The c_str() and strcpy() function in C++C++ c_str() function along with C++ String strcpy() function can be used to convert a string to char array easily. The c_str() method represents the sequence of characters in an array of string followed by a null character ('\0').
Depends. If it is guaranteed that setBuiltIns
does not modify the strings, you can just const_cast
the const
away.
Otherwise, choose the always safe path and copy the literals into new buffers.
As requested, an example for the first variant:
void fun(char *builtins[]){}
int main () {
const char *delegates[] = {"IN",nullptr};
fun(const_cast<char **>(delegates));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With