0

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)

  • why is that? it has only affected me recently and I have been using VS2017/2015 for a long time. And your example just blew my mind, I have always been able to do that in VS2017 until I reinstalled everything – Reece Ward Dec 31 '17 at 00:37
  • Ah maybe so, it kinda took me off guard – Reece Ward Dec 31 '17 at 00:40
  • I provided another example where adding the const doesn't work – Reece Ward Dec 31 '17 at 00:56
  • @ReeceWard Please post a [mcve] that duplicates the error. [There is no error when I use dummy classes](https://www.ideone.com/Fy3Vow). – PaulMcKenzie Dec 31 '17 at 01:12
  • The code from @PaulMcKenzie compiles for me under Visual Studio 2017. Although I don't have the latest update. – drescherjm Dec 31 '17 at 01:23
  • I found that enabling `/permissive-` causes the original error in Visual Studio 2017. https://learn.microsoft.com/en-us/cpp/build/reference/permissive-standards-conformance – drescherjm Dec 31 '17 at 02:28
  • Possible duplicate of [Conversion from String Literal to Char* is deprecated](https://stackoverflow.com/questions/13690306/conversion-from-string-literal-to-char-is-deprecated) – xskxzr Dec 31 '17 at 07:46

1 Answers1

2

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);
O'Neil
  • 3,790
  • 4
  • 16
  • 30
SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23