C ++ - How to add char to char *?

I tried so that on the Internet you can add a character to char *, but none of them work. Here is one of my incomplete solutions:

char* appendCharToCharArray(char * array, char a) { char* ret = ""; if (array!="") { char * ret = new char[strlen(array) + 1 + 1]; // + 1 char + 1 for null; strcpy(ret,array); } else { ret = new char[2]; strcpy(ret,array); } ret[strlen(array)] = a; // (1) ret[strlen(array)+1] = '\0'; return ret; } 

This only works when the passed array is "" (empty inside). Otherwise, this will not help (and received an error with (1)). Could you guys help me with this? Thanks so much for the advanced!

+6
source share
4 answers

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'); // or str += x; 
+11
source

The name of the function does not reflect the semantics of the function. You are not actually adding a character. You create a new character array containing the original array plus the specified character. Therefore, if you really need a function that adds a character to an array of characters, I would write it as follows.

 bool AppendCharToCharArray( char *array, size_t n, char c ) { size_t sz = std::strlen( array ); if ( sz + 1 < n ) { array[sz] = c; array[sz + 1] = '\0'; } return ( sz + 1 < n ); } 

If you need a function that will contain a copy of the original array plus the specified character, then it may look like this.

 char * CharArrayPlusChar( const char *array, char c ) { size_t sz = std::strlen( array ); char *s = new char[sz + 2]; std::strcpy( s, array ); s[sz] = c; s[sz + 1] = '\0'; return ( s ); } 
+2
source

The specific problem is that you are declaring a new variable instead of assigning an existing one:

 char * ret = new char[strlen(array) + 1 + 1]; ^^^^^^ Remove this 

and tries to compare string values ​​by comparing pointers:

 if (array!="") // Wrong - compares pointer with address of string literal if (array[0] == 0) // Better - checks for empty string 

although there is no need to make this comparison at all; the first branch will do the right thing whether the row is empty.

The more common problem is that you mess with nasty, error-prone C-style string manipulations in C ++. Use std::string and it will manage the entire memory allocation for you:

 std::string appendCharToString(std::string const & s, char a) { return s + a; } 
+1
source
 char ch = 't'; char chArray[2]; sprintf(chArray, "%c", ch); char chOutput[10]="tes"; strcat(chOutput, chArray); cout<<chOutput; 

OUTPUT:

 test 
0
source

Source: https://habr.com/ru/post/957777/


All Articles