What is this?

1
2
3
4
5
6
string string1("Confused");
int length = string1.length();
char *ptr1 = new char[length +1];
*ptr1 = 0;
string1.copy(*ptr1, length, 0);
*ptr1[length] = '/0';


string1.copy(*ptr1, length, 0);

What is the point of that trailing zero? is that a placer?
Last edited on
1
2
string1.copy(*ptr1, length, 0);
*ptr1[length] = '/0';


Neither of these lines would compile. The correct code is probably this:

1
2
string1.copy(ptr1, length, 0);  // no *'s
ptr1[length] = '/0';


The trailing zero is the null terminator. It marks the end of the string... since char arrays don't have 'length' members like the string class does -- it's just assumed they continue on until a null character (0) is found.
Last edited on
Topic archived. No new replies allowed.