>is there's any way to define the last character or to detect the end of a dynamicly allocated array?
You already know the end of a dynamically allocated array because you allocated it and the allocation requires a size. To use strlen, you just copy a '\0' character to the last element:
Code:
#include <cstring>
#include <iostream>
int main()
{
char *p = new char[11];
// Fill the array
for ( int i = 0; i < 10; i++ )
p[i] = i + '0';
// Tack a null character on the end
p[10] = '\0';
// Now it all works
std::cout<< p <<" -- "<< std::strlen ( p ) <<'\n';
}
But you have to be very careful to only place '\0' after valid characters or you'll end up looking at indeterminate data. In other words, this is bad:
Code:
#include <cstring>
#include <iostream>
int main()
{
char *p = new char[11];
// Tack a null character on the end
p[10] = '\0';
// Ack! Undefined behavior!
std::cout<< p <<" -- "<< std::strlen ( p ) <<'\n';
}