Hi,
I need to store a char array inside a class and then return it. I have to admit that I'm a bit confused about pointers and have tried everything I can think of but can't get it to work. Here's what I have:
#include <iostream>
using namespace std;
class Test {
public:
void setName(char *name);
char getName();
private:
char m_name[30];
};
void Test::setName(char *name) {
strcpy(m_name, name);
}
char Test::getName() {
return *m_name;
}
void main() {
Test foobar;
char name[] = "Testing";
foobar.setName(name);
cout << foobar.getName();
}
Of course, I expect setName() to store the string "Testing" inside the class, and getName() should return "Testing". But instead, I get only the first letter T. What am I doing wrong?
I guess I should be using std strings but first I would like to understand why this does not work. As far as I know, this should work with char arrays as well?