简体   繁体   中英

How to supply a 'const char*' argument for a function that takes a 'char*' in C++?

I have the following code.

const char* my_input = "my string";

void my_function(char* my_argument);

int main()
{
  my_function(my_input);
}

void my_function(char* my_argument)
{
  /* Do something */
}

Notice that the argument for my_function expects a 'char*', but I supply it with a 'const char*'.

My IDE gives me the error: No matching function for call 'my_function'.

I assume it is because the function call is being interpreted as 'my_function(const char* my_argument)', which I have clearly not defined (and do not want to).

How do I can I make it so that my constant input is valid for an expected non-constant argument?

In my real program, I have a function that takes in a 'char*', however it does NOT modify it. The argument itself might not be a constant (and isn't usually), however sometimes I expect a constant.

Here, the stablest solution would be to make your input a char* rather than a const char* :

char my_input[] = "my string";

However, if you cannot, another solution would be to use const_cast to "reassure" your IDE on the parameter:

int main()
{
    my_function(const_cast<char*>(my_input));
}

But be careful with this kind of tricks: if your variable is read-only and your function my_function try to modify it, be ready for a segmentation fault.

You can do that if you are certain your C function won't try to do anything but read the pointer's data, even if the argument is not const , as it happens sometimes in C libraries.
Else, you have to find a way to pass a modifiable parameter.

If your data is const , a more heavy but secure way would be to copy your original pointer into a new one, modifiable one:

int main()
{
    char* copy = strdup(my_input); // Or a static buffer if you know precisely your input size
    my_function(copy);
    free(copy);
}

It is not being interpreted as my_function(const char *) call. It is being interpreted as my_function(char *) call, since there's no other versions of my_function available anywhere. However, this is not allowed because it violates the rules of const-correctness of C++ language. These rules prohibit implicit conversion of const char * pointer to char * type.

The first question you have to answer here is why you are trying to pass a pointer to constant data to a function that potentially treats it as non-constant (modifiable) data. This is an obvious violation of const-correctness.

If your my_function does not really modify the data, then the next question is why it is declared with char * parameter instead of const char * parameter.

And only as the last resort, if you are sure that my_function does not really modify the data, but for some reason cannot change its declaration (eg third party code), you can work around this problem by forcing the conversion to char * type

my_function(const_cast<char *>(my_input));

You can use const_cast :

my_function(const_cast<char*>(my_input));

However, this is dangerous, because if my_function modifies my_argument , it is undefined behavior.

As you know, a const variable cannot be modified, so you cannot pass it to a function that may change its value.

If you need to pass the value of that variable, then the best way to do it is creating a writable non-const copy.

char my_copy[strlen(my_input) + 1];
strcpy(my_copy, my_input);
my_function(my_copy);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM