How to provide a 'const char *' parameter to a function that takes a 'char *' in C++?


good man

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 */
}

Note that the argument to my_function expects a 'char *', but I gave it a 'const char *'.

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

I'm guessing because the function call is being interpreted as "my_function(const char *my_argument)", which I obviously haven't defined (and don't want to).

How can I make my constant input valid for the expected non-constant parameter?

In my real program, I have a function that accepts 'char *', but it doesn't modify it. The parameter itself may not be a constant (usually it isn't), but sometimes I expect a constant.

Ant

It is not interpreted as a my_function(const char *)call. It is interpreted as my_function(char *)calling because there is no other version available my_functionanywhere . However, this is not allowed because it violates the const-correctity rules of the C++ language. These rules prohibit implicit conversions of const char *pointers to char *types.

The first question you have to answer here is why you're trying to pass a pointer to constant data to a function that might treat it as non- const (modifiable) data . This is a clear violation of const correctness.

If you're my_functionnot actually modifying the data, the next question is why declare the data with char *parameters instead of const char *parameters .

And only as a last resort, if you're sure the data won't actually be modified, but its declaration can't be changed for some reason (e.g. third-party code), you can fix this my_functionby casting the cast to the char *type .

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

Related


Pass const char* to function in C

Dan Brenner I'm trying to write a function that takes a const char *as a parameter, but I don't know how to pass such data to the function in a useful way. If I have the function: void tokenize(const char * c) { } I want to call this function with a hardcoded

Pass const char* to function in C

Dan Brenner I'm trying to write a function that takes a const char *as a parameter, but I don't know how to pass such data to the function in a useful way. If I have the function: void tokenize(const char * c) { } I want to call this function with a hardcoded

Pass const char* to function in C

Dan Brenner I'm trying to write a function that takes a const char *as a parameter, but I don't know how to pass such data to the function in a useful way. If I have the function: void tokenize(const char * c) { } I want to call this function with a hardcoded

How to return const char* in a function?

username I am giving me my entire code. It's a bit long, so I most sincerely hope you get: #include <iostream> using namespace std; char const* reverseWordsOnly(const char* s) { int k; int p = strlen(s); for (int i = 0; i <= p; i++) {

How to return const char* in a function?

username I am giving me my entire code. It's a bit long, so I most sincerely hope you get: #include <iostream> using namespace std; char const* reverseWordsOnly(const char* s) { int k; int p = strlen(s); for (int i = 0; i <= p; i++) {

How to pass const char* as parameter of LPCSTR parameter?

Gábor Marschall I want to use this function to call the PlaySoundA function, but the sound file I want to play doesn't open. void AudioClass::playAudio(const char* incomingData, const char* filePath) { char buffer[100]; // <- danger, only storage for 100

How to pass const char* as parameter of LPCSTR parameter?

Gábor Marschall I want to use this function to call the PlaySoundA function, but the sound file I want to play doesn't open. void AudioClass::playAudio(const char* incomingData, const char* filePath) { char buffer[100]; // <- danger, only storage for 100

How to pass const char* as parameter of LPCSTR parameter?

Gábor Marschall I want to use this function to call the PlaySoundA function, but the sound file I want to play doesn't open. void AudioClass::playAudio(const char* incomingData, const char* filePath) { char buffer[100]; // <- danger, only storage for 100

How to pass const char* as parameter of LPCSTR parameter?

Gábor Marschall I want to use this function to call the PlaySoundA function, but the sound file I want to play doesn't open. void AudioClass::playAudio(const char* incomingData, const char* filePath) { char buffer[100]; // <- danger, only storage for 100

How to pass const char* as parameter of LPCSTR parameter?

Gábor Marschall I want to use this function to call the PlaySoundA function, but the sound file I want to play doesn't open. void AudioClass::playAudio(const char* incomingData, const char* filePath) { char buffer[100]; // <- danger, only storage for 100

How to pass const char* as parameter of LPCSTR parameter?

Gábor Marschall I want to use this function to call the PlaySoundA function, but the sound file I want to play doesn't open. void AudioClass::playAudio(const char* incomingData, const char* filePath) { char buffer[100]; // <- danger, only storage for 100

How to pass const char* as parameter of LPCSTR parameter?

Gábor Marschall I want to use this function to call the PlaySoundA function, but the sound file I want to play doesn't open. void AudioClass::playAudio(const char* incomingData, const char* filePath) { char buffer[100]; // <- danger, only storage for 100

How to pass const char* as parameter of LPCSTR parameter?

Gábor Marschall I want to use this function to call the PlaySoundA function, but the sound file I want to play doesn't open. void AudioClass::playAudio(const char* incomingData, const char* filePath) { char buffer[100]; // <- danger, only storage for 100

How to convert const char* to char* in C?

Morpheus: In my project there is a method that just returns a const char*and I need a char*string because the API doesn't accept it const char*. Any ideas how to convert const char*between char*? Meaning matters: To be on the safe side, don't break things (e.g

How to convert char* to char*const* in C

Good night I am trying to use the execv() function. I am trying to pass my parameter command to the left. execv(file,arguments); I'm using char* to parse my shell's input user input. The second parameter of execv takes a char *const*. Is there a way to cast

How to convert const char* to char* in C?

Morpheus: In my project there is a method that just returns a const char*and I need a char*string because the API doesn't accept it const char*. Any ideas how to convert const char*between char*? Meaning matters: Just to be on the safe side, don't break things