G
Gandalf
Hello.
I thought that strlen was only available after adding
#include <string>
to my c++ program, but apperently not, or the compiler is
clever enough to find it by it self, or what?
The program compiles and runs fine on my Linx box, with
g++ version 2.95.4
Second question. Wouldn't the method, Set, result in that the char* text
will point to a newly allocated char array that is one char too short?
strlen ignores the final nullchar, and then the final null char will be
omitted in the copy procedure?
Regards.
----------------------
#include <iostream>
class Problem {
private:
char* text;
public:
Problem() {text=new char[1]; text='\0';}
void prt() {cout<<text;}
void Set(char* str) {delete text; text=new char[strlen(str)];
strcpy(text,str);}
};
int main() {
Problem a;
Problem b;
a.Set("Gandalf");
a.Set("Bigger Longer Uncut text");
a.prt();
}
I thought that strlen was only available after adding
#include <string>
to my c++ program, but apperently not, or the compiler is
clever enough to find it by it self, or what?
The program compiles and runs fine on my Linx box, with
g++ version 2.95.4
Second question. Wouldn't the method, Set, result in that the char* text
will point to a newly allocated char array that is one char too short?
strlen ignores the final nullchar, and then the final null char will be
omitted in the copy procedure?
Regards.
----------------------
#include <iostream>
class Problem {
private:
char* text;
public:
Problem() {text=new char[1]; text='\0';}
void prt() {cout<<text;}
void Set(char* str) {delete text; text=new char[strlen(str)];
strcpy(text,str);}
};
int main() {
Problem a;
Problem b;
a.Set("Gandalf");
a.Set("Bigger Longer Uncut text");
a.prt();
}