Hello
I am trying to copy a string that doesn't have a fixed size.
What is the way to do it?
Thank you
char *search, *split;
......
search = split; doesn't work
strcpy(search, split); doesn't work
Hello
I am trying to copy a string that doesn't have a fixed size.
What is the way to do it?
Thank you
char *search, *split;
......
search = split; doesn't work
strcpy(search, split); doesn't work
Looks like the so-called string isn't null terminated otherwise strcpy() would have worked.
Post more details about the string as it is not easy to figure out what you are getting at.
Well, I am trying to copy the string from buff to search.
I get a seg fault at the strcpy line.
I want to adapt the "search" string and leave the "buf" string intact
Code:char *search, *buf; buf = ("Hello"); search = NULL; printf("buf: %s\n", buf); strcpy(search, buf); printf("search: %st\n", search); strcat(search, " "); printf("buf: %st\n", buf); printf("search: %st\n", search);
You have to allocate memory first before copying into search.
Thank you !!
sizeof(char) is for clarity, the compiler will(or should) ignore it since it evals to a constant 1.Code:char *search, *split; ...... strcpy((search = malloc((strlen(split) + 1) * sizeof(char))), split);