char array in a class

closed account (Sy0XoG1T)
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?
Use a std::string ( you need to #include<string> for that )
closed account (Sy0XoG1T)
I'm aware of strings but I'd still like to know if there was a solution to my problem. I appreciate your answer though. :)
closed account (Sy0XoG1T)
So I would basically be forced to use some sort of function. Thanks for the help. :D
1
2
3
4
5
6
7
8
9
class TEST
{
public:
      // give it a constructor
      TEST() : myString("hello")
      {
       }
       const char* 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.
Topic archived. No new replies allowed.