Hi In my code i have a string stored in char* i want to copy only some part of the string to another char*. say for example,
char * str1 = "I am new to c++";
// I am creating new char pointer
char * str2 = new char[20];
int startpos = 5;
int destpos = 0;
// i am checking for the white space from the start posistion
while(str1[startpos]!=' '){
str2 [destpos++] = str1[startpos++];
}
printf("\n New String is %s",str2);
// now i am deleting the char*
delete[] str2;
delete[] str1;
I am using this kind of manipulation and this code runs properly sometimes and causes seg fault (double free or corruption).
Someone please tell me what is the reason.