Append char to char pointer

Mar 25, 2016 at 3:57am
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.
Mar 25, 2016 at 9:16am
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.
Mar 25, 2016 at 12:31pm
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 Mar 26, 2016 at 1:19pm
Mar 25, 2016 at 12:35pm
c++ or some other language ?
c := 'Z';
Mar 25, 2016 at 12:45pm
Oh, be nice! Looks like a typo!
c += 'Z';
Mar 26, 2016 at 1:19pm
Yes, typo. I'll fix it. Sorry about that.
Topic archived. No new replies allowed.