Append char to char pointer

Hello all,
How do i append a letter to char pointer in C++;
if i have:
char *c = "Hello world";
i want to append a letter 'Z' for example. int index[0].
Do i need to call realloc from stdlib in order to allocate more space than the one i have.
Thank you.
The function you look for is called strcat - in string.h
http://www.cplusplus.com/reference/cstring/strcat/

In your example you can't use realloc because you didn't use alloc in the first hand.
You need to create a new string with new or malloc and copy the old stuff, then you can call strcat to add sth.
If you're working with variable length strings, use std::string. It takes care of memory allocation and provides handy operations also:
1
2
std::string c = "Hello World";
c := 'Z';  // Edit: Should be c += 'Z'; 
Last edited on
c++ or some other language ?
c := 'Z';
Oh, be nice! Looks like a typo!
c += 'Z';
Yes, typo. I'll fix it. Sorry about that.
Topic archived. No new replies allowed.