FoioLag
FoioLag

Reputation: 59

how to pass a parameter to char*?

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

Upvotes: 0

Views: 644

Answers (2)

asmmo
asmmo

Reputation: 7100

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

Upvotes: 4

Jarod42
Jarod42

Reputation: 217275

You have incorrect constness, it should be:

void PrintString(const char* s)

Upvotes: 2

Related Questions