I am trying to create a simple string class, but i am getting a run time error. What i want to accomplish is to store a const char pointer to a char pointer. I am trying to use the strncpy function, i also tried the memcpy, but both gave the apparently the same error. The src argument is getting a value, however dst isn't, it says it's a bad pointer. I also tried using strcpy, but i can't allocate memory with malloc, it says that i can't assing void pointer to char pointer.
This is my header:
class MyString{
private:
char* thestring;
public:
MyString();
MyString(const MyString& str);
MyString(const char * str);
~MyString();
MyString& operator=(const MyString& str);
MyString& operator=(const char * str);
};
This is my cpp:
MyString::MyString(){
thestring = '\0';
}
MyString::MyString(const MyString& str){
thestring = strupr(str.thestring);
}
MyString::MyString(const char * str){
memcpy(thestring,str,strlen(str) + 1);
//strncpy(thestring,str,sizeof *str + 1);
}
MyString& MyString::operator=(const MyString& str){
if (this != &str){
free(thestring);
thestring = strupr(str.thestring);
}
return *this;
}
MyString& MyString::operator=(const char * str){
if (&this->thestring != &str){
//strncpy(thestring,str,sizeof *str + 1);
memcpy(thestring,str,strlen(str + 1);
}
return *this;
}
MyString::~MyString(){
free(thestring);
}