-1

I'm new here and I wish that you can resolve this question, it is a constant error or omission on my programs when I use a data type char on referentiation in objects derived of struct's or classes. This is my source. Thanks in advance.

 struct test{
    public:

        char cadena[20];
        int num;

   }objeto;

  int main(){

    test *POJ = &objeto;
    (*POJ).cadena="Hello Guys";
    POJ->num=23;
    cout<<(*POJ).cadena<<", "<<POJ->num;

  }
Binar Web
  • 867
  • 1
  • 11
  • 26
Decameron
  • 15
  • 1
  • 5
    You need `strcpy()`. After all I'd recommend using `std::string`. – πάντα ῥεῖ Jun 05 '16 at 19:40
  • The more common syntax for `(*POJ).cadena` is `POJ->cadena`. You cannot assign a `char *` to a `char []` like that. Use `std::string` instead, it has proper overloads to make that work. It will probably be easier to [read a book](http://stackoverflow.com/a/388282/3484570) instead of asking here. – nwp Jun 05 '16 at 19:43

1 Answers1

7
(*POJ).cadena="Hello Guys";

cadena is an array. An array cannot be assigned to.

Either make cadena a std::string, which is what all self-respecting C++ code will use to store text string, or use the C strcpy() function to copy raw character data into a plain char array.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148