Just noticing a few things (using WaltP's code as a reference):
1) using sizeof(buffer)
will not give you the right length since buffer is a pointer, so it'll probably be the same size as an int. You don't want actualMessage to be 4 chars long (assuming an int is 4 bytes). You should use strlen(buffer)
rather than sizeof(buffer)
.
2) memory allocated needs to be deleted. ;)
[edit:] here's an example of what I mean in part 1:
#include <stdio.h>
void foo(char* c)
{
printf("size: %d\n", sizeof(c));
}
int main()
{
char array[128] = {0};
printf("size: %d\n", sizeof(array));
foo(array);
return 0;
}
output:
$ ./a.out
size: 128
size: 4
[edit 2:] Also noticed: the OPs method of allocating a dynamic array on the stack is only supported by a couple compilers and isn't very portable. Probably want to use new/delete (or malloc/free) instead, as the other replies have done.