Home » C++ ProgrammingRSS

Argument of type 'const char*' is incompatible with parameter of type 'char*'

What does this error mean and how can I solve it? 

"argument of type 'const char*' is incompatible with parameter of type 'char*'"

I have a C++ method defined as void DoSomething(char* data)

and I'm trying to call it like this: DoSomething("Hello " + UserName);

 

 

7 Answers Found

 

Answer 1

>I have a C++ method defined as void DoSomething(char* data)
>I'm trying to call it like this: DoSomething("Hello " + UserName);

That won't work for starters: You can't join C strings together
using the plus operator.

>"argument of type 'const char*' is incompatible with
>parameter of type 'char*'"
>What does this error mean ...?

What it says. Look up the error in the compiler's help.
You're trying to pass a pointer to const char to a
function which expects a pointer to char (non-const).

- Wayne

 

Answer 2

I don't understand.  I'm new to C++ and I'm trying to follow an example.  Can you give an example of how to solve this in the function call? - I can't change the function definition.
 

Answer 3

Tthe problem you described is no C++ problem, it is a normal C problem. You can try this for example:

 

char buffer[80]; // a 80 character buffer

...

strcpy(buffer, "Hello ");
strcat(buffer, UserName);
DoSomething(buffer);

 

 

Answer 4

Hello DeveloperPerson :D

when I saw you're question I remembered the times when I was writing my first C++ programs and always forgot to use " using namespace std" :D

back to the problem.

"argument of type 'const char*' is incompatible with parameter of type 'char*'"

In general when you get a problem like this it means that you are trying to put a parameter of a type (in this case "const char*" ) that is incompatible or not convertible to the parameter type the function is expecting . So you're function ( DoSomething(char* data) ) is expecting char* and you pas to it  "Hello " + UserName whichis const char*.

In C/C++ a string like "Hello" has the type of const char* so you can't directly use it for a char* parameter. You ca although do a cast like DoSomething((char*)"Hello"));

And as it was already written you can not concat 2 char* strings using +.   Use std::string ( #include<string> ) if you want to do something like:

#include<string>

using namespace std;

.......

........

string userName = "XYZ";

string str = "hello" + userName;

 

Answer 5

Just a few additional things you need to become familiar with -

Avram F has shown you how you can create a C++ string object
and add to it by using the plus operator. However since your
function expects a pointer to char (char *) it will not accept
a C++ string object, whether passed by value, reference or
pointer.

C++ string objects have a member function which allows you
to return a C-style string pointer (char *) to the data in
the C++ string object:

string userName = "XYZ";

printf("%s\n", userName.c_str());

However, note that the string returned should always be
treated as if it were a const char * and should never
be written to via the pointer returned. This can corrupt
the string object's internals. Always use the string class
methods provided for altering the data in the string
object.

The C++ String Class
http://www.cprogramming.com/tutorial/string.html

Lesson 9: C Strings
http://www.cprogramming.com/tutorial/c/lesson9.html

- Wayne

 

Answer 6

Thanks.  These responses helped a lot!
 

Answer 7

What does this error mean and how can I solve it? 

"argument of type 'const char*' is incompatible with parameter of type 'char*'"

I have a C++ method defined as void DoSomething(char* data)

and I'm trying to call it like this: DoSomething("Hello " + UserName);

 


Ignoring the other problems with your argument expression, "Hello" is a string literal.  The language definition does not allow you to modify string literals.  (Otherwise, attempting to use the same literal in another part of your function would produce undesirable results.)  Consequently, the array holding the literal has the const qualifier as part of its type.  When you pass an array like this to a function, the array expression is converted to the address of the first element with type pointer to element type.  Since your array is const char[], each element has type const char.  Therefore the expression has type const char*.  Your function is expecting a (non-const) char*.  As such it would be legal for the function to change the contents of the elements of the array.  But that is not allowed when the the array is a const.  The net result is that there is no implicit conversion from (pointer to const) to (pointer).  The compiler is telling you that you are not allowed to pass a const array to a function which might try to modify that array.

It does not matter whether your function actually alters the array, only that it might.  If your function never alters the array, then you can declare it with a const parameter and your call will work.  (Incidentally, the call will work even if the array is not const becasue there is an implicit conversion from (pointer) to (pointer to const) since it is not a problem to pass a modifiable array to a function that will not modify it.)  On the other hand, if your funciton can modify the array, you need to copy the const array to an array you can modify and pass this copy to your function.

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter