I'm having some trouble using an array of char's inside a class. For example:
1 2 3 4 5 6 7 8 9 10 11 12
class TEST
{
public:
char mystring[20];
}TEST_CLASS;
int main()
{
TEST_CLASS.mystring = "Hello";
system("pause");
return 0;
}
The compiler complains that it cannot convert a const char [6] into a char [20]. I am aware that strcopy works perfectly for this situation but I'd like to stay away from using a function for such a simple task. Any solutions or is strcopy my best bet?
class TEST
{
public:
// give it a constructor
TEST() : myString("hello")
{
}
constchar* myString;
}TEST_CLASS;
Your array should be const since a string literal is a const. The problem with what you are doing is you are assigning an arry the address of a string literal which makes it impossible to change the string anyway. Initializing a 20 element array like that is a waste and it would only give you a false sense that you can change the contents later when you can't.