Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deprecated conversion from string constant to 'char*' [duplicate]

Tags:

c++

string

char

Possible Duplicate:
C++ deprecated conversion from string constant to 'char*'

I want to pass a string via char* to a function.

 char *Type = new char[10];  Type = "Access";  // ERROR 

However I get this error:

 error: deprecated conversion from string constant to 'char*' 

How can I fix that?

like image 479
mahmood Avatar asked Nov 14 '11 18:11

mahmood


1 Answers

If you really want to modify Type:

char *Type = new char[10]; strcpy( Type, "Access" ); 

If you don't want to modify access:

const char *Type = "Access"; 

Please note, that, however, arrays of char in C and in C++ come with a lot of problems. For example, you don't really know if the call to new has been successful, or whether it is going to throw an exception. Also, strcpy() could surpass the limit of 10 chars.

So you can consider, if you want to modify type later:

std::string Type = "Access"; 

And if you don't want to modify it:

const std::string Type = "Access"; 

... the benefit of using std::string is that it is able to cope with all these issues.

like image 190
Baltasarq Avatar answered Oct 01 '22 08:10

Baltasarq