+ 1
How Can I use setters and getters in a char array? c++
I'm so used to work with std::string, but, I'm not sure how to use a char array in the getters and setters; I have this little code: char name[10] // I think is the same as char* name void setName(char* n) { name = n; } // ERROR is not assignable. char* getName() const { return name; } Ass you can see, I'm not sure how to get and set the name properly, how can I solved this?
6 Respuestas
+ 4
You will have to use some of the built in functions for char strings like strcpy for setting:
{ strcpy(n, name); }
+ 1
Zeke Williams thx, at the end, I got the same answer, using strcpy, now is working.
~ swim ~ well, I know that using std::string avoids all this, but I'm working on files, my teacher recommend me to use the C style to write and read binary files.
0
so I assume that create *char allocates in heap right?