Remove those char * ret declarations inside if blocks that hide the outer ret . For this, you have a memory leak, and on the other hand, unallocated memory for ret .
To compare a c-style string, you should use strcmp(array,"") not array!="" . Your last code should look like this:
char* appendCharToCharArray(char* array, char a) { size_t len = strlen(array); char* ret = new char[len+2]; strcpy(ret, array); ret[len] = a; ret[len+1] = '\0'; return ret; }
Note that you must process the allocated memory of the returned ret somewhere on delete[] .
Why aren't you using std::string ? it has an .append method to add a character at the end of a line:
std::string str; str.append('x');
source share