Reece Ward
Reece Ward

Reputation: 1

C++ Strange char* parameter issue (incompatible types)

I'm having a slight issue with char* as parameters. I've never had this issue before but I can't seem to find why it's happening now. A slight example of what "should be perfectly fine":

void foo(char* param)
{
    return;
}

foo("hello world");

This doesn't work because we get the error:

cannot convert argument 1 from 'const char [12]' to 'char *'

I had a look around but couldn't find anything. I've also tried changing the character set but that went nowhere. I created a new project but the same thing happened. Have I changed a setting somewhere maybe?

I attempted to add the const which worked for the basic example but not for my templates

void Setup()
{
    Find<MyClass>("function.dll", "function");
}

template <class i>
i* Find(const char* module, const char* name)
{
    return nullptr;
}

This gives me something else I've never seen too:

Conversion from string literal loses const qualifier (see /Zc:strictStrings)

Upvotes: 0

Views: 250

Answers (1)

SoronelHaetir
SoronelHaetir

Reputation: 15162

String literals are char const *, not char *. The compiler is correct here. If you want to have a modifiable char array you can do:

void foo(char *) { }
char buf[] = {"hello, world"};
foo(bar);

Upvotes: 2

Related Questions