chapman
chapman

Reputation: 929

Appending a single character to a dynamic character array in C

How do I append a character to a char*? So...

char* thingy = "test";
char* another = "hello world";

thingy += another[6];

printf("%s\n", thingy);

I wan't the output to be:

testw

However, I get this output

at address %p

edit:

Thanks for the help :)

Upvotes: 0

Views: 2359

Answers (4)

Mario
Mario

Reputation: 36487

There's no string arithmetic in C, so you can't do it that way.

However, there's strcat(), which you can use (as long as there's room for those characters):

char thingy[256] = "Hello World";

strcat(thingy, "!");

// thingy is now "Hello World!"

Although it's important to note that you should always check string lengths and be careful when doing such things.

If you'd like to add a single character rather than a string, you can either copy that character to a string:

char thingy[256] = "Hello World";

char dummy[] = "#";

dummy[0] = '!';
strcat(thingy, dummy);

// thingy is now "Hello World!"

Or do it the manual way:

char thingy[256] = "Hello World";

unsigned int len = strlen(thingy);

thingy[len] = '!';      // Append character
thingy[len + 1] = '\0'; // Readd termination

// thingy is now "Hello World!"

Upvotes: 2

stix
stix

Reputation: 1146

Your only option in this case is to reallocate the char*'s memory so that you can get a larger string.

First, you will need the length of the original string, then you must add 1 to it as the strlen function does not include the null terminator:

char* thingy = "test";
char* another = "hello world";

int len = strlen(thingy);
char* thingy = realloc(thingy, (len + 2) * sizeof(char));
thingy[len] = another[6];
thingy[len +1] = '\0';

printf("%s\n", thingy);

If you have access to C++, however, a better approach is to use the std::string object:

std::string thingy = "test";
std::string another = "hello world";

thingy += another[6];
printf("%s\n", thingy.c_str());

Since strings are containers, there are myriad ways to approach the problem:

thingy.push_back(another[6]);

thingy.append(another, 6, 1);

thingy.insert(thingy.end(), another[6]);

Another benefit of std strings is that they handle the null terminator for you, so you don't have to worry about it.

Upvotes: 1

Arkku
Arkku

Reputation: 42129

thingy is a pointer (*) to char, i.e., the address of the first character of the string "test". Arithmetic on thingy changes the address to which it points:

thingy += another[6];

This adds the integer value of the char at the address another + 6 to the address pointed to by thingy. This is beyond the end of the string "test" and thus undefined behaviour - it just happens that your program has the string "at address %p".

Also, the string pointed to by thingy is a constant so you cannot append to it. You could make it an array, e.g., char thingy[MAX_LENGTH_OF_THINGY] = "test"; instead, and then do something like thingy[4] = another[6]; thingy[5] = '\0'; (note the need to NUL-terminate C-strings). Or you can create a new string altogether, e.g., by mallocing enough memory and copying the original + the additional character to it.

Upvotes: 0

Ben
Ben

Reputation: 2123

The string you append onto should not be a literal. If you had:

char thingy[10] = "test";

You can:

int len = strlen(thingy);
thingy[len] = another[6];
thingy[len+1] = '\0';

Upvotes: -1

Related Questions