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

Viewed 4389

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.

4 Answers
Related