Warm tip: This article is reproduced from stackoverflow.com, please click
c++ string

how to pass a parameter to char*?

发布于 2020-10-11 11:20:00

As in the code below, I can't pass this parameter, how do I fix it?

E0167 The "const char *" type argument is incompatible with the "char *" type parameter

Code example:

#include <iostream>
using namespace std;

int PrintString(char* s)
{
    cout << s << endl;
}

int main(int argc, char* argv[])
{
    PrintString("TESTEEEE");
    return 0;
}

I've already tried PrintString(L"TESTEEEE");

I've also tried setting the Project -> Properties -> General -> Character Set option to use Multi-Byte Character Set.

error image

Questioner
FoioLag
Viewed
4
193k 2020-06-19 05:42

This literal "TESTEEEE" is of type char const[9]. When used as an argument to a function, it can decay to char const* but not to char*. Hence to use your function, you have to make the parameter fit to your argument or the opposite as follows

#include <iostream>

using namespace std;
int PrintString(const char* s)
{
    cout << s << endl;
}


int main(int argc, char* argv[])
{

    PrintString("TESTEEEE");

    return 0;
}

live

OR

#include <iostream>

using namespace std;
int PrintString( char* s)
{
    cout << s << endl;
}


int main(int argc, char* argv[])
{
    char myArr[] = "TESTEEEE";
    PrintString(myArr);

    return 0;
}

live