How Can I use setters and getters in a char array? c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

31st Aug 2019, 2:16 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
3 Answers
+ 4
You will have to use some of the built in functions for char strings like strcpy for setting: { strcpy(n, name); }
31st Aug 2019, 2:35 PM
Zeke Williams
Zeke Williams - avatar
+ 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.
31st Aug 2019, 8:01 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
0
so I assume that create *char allocates in heap right?
31st Aug 2019, 2:50 PM
BinaryEden
BinaryEden - avatar