PDA

View Full Version : [ANSI C] string manipulation problem



roccivic
October 3rd, 2009, 04:35 PM
I would like to append a string to another string. The below code does exactly that, but it places the result in both "str1" and "str2" (for obvious reasons).
Is there any way of accompliscing this task and place the result just in "str2" without affecting "str1"?

Many thanks for any help in advance.

Rouslan


#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main (void) {
char * str1 = getenv("HOME");
char * str2 = strcat(str1, "/foo");

printf("\nstr1: %s\nstr2: %s\n", str1, str2);

return 0;
}

MadCow108
October 3rd, 2009, 04:40 PM
char * str1="bla";
char str2[100];
snprintf(str2,100,"%s/foo",str1);

roccivic
October 3rd, 2009, 04:42 PM
Good man yourself,
Thanks