Hi!
I have two strings:
char str_1[] = "First String ";
char str_2[] = "Second String";
how do I copy the second string at the end of first string, so the result would be:
"First String Second String"
Thank you!
Hi!
I have two strings:
char str_1[] = "First String ";
char str_2[] = "Second String";
how do I copy the second string at the end of first string, so the result would be:
"First String Second String"
Thank you!
By the way you have done it, you cannot. You'll have to have a third var that is large enough to hold both str_1 and str_2 with the '\0' at the end. Then, however, you could use strcat() or sprintf().
EDIT: Or, you could make str_1[50] so that it would give you enough space.
Kennedy is correct. I think you could also try making them both pointers, or at least make the first one a pointer.
char * str_1 = "First String ";
char * str_2 = "Second String";
That way you don't have to worry about how big str_1 is. Isn't there a string function strcat() that will do what you want? I believe that takes two strings as arguments, and appends one to the end of the other. I could be wrong though.
Nope, that still won't work. You gotta have more memory. You'll _HAVE_ to make str_1 bigger some way, whether by malloc() (using a char *) or statically defining it as a bigger chunk of memory.
> That way you don't have to worry about how big str_1 is
Not only do your pointers point to strings in read-only memory (on any decent 32 bit platform anyway, so even trying to change them is fatal), you ALWAYS have to worry about how much memory you have to play with.
There is no such thing as a pointer to an infinite amount of memory.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
well this will do what u want. Had a sample code before. This is just a sample code
dont forget to free the str1 and str2Code:str1 = malloc(sizeof(char) * 10); str2 = malloc(sizeof(char) * 10); strcpy(str1,"hello"); strcpy(str2,"world"); str1 = realloc( str1 , sizeof(char) * strlen(str2) ); strcat(str1,str2);
ssharish2005
Nope, this won't work either. You have to have a memory location big enough to handle BOTH strings. i.e. (to modify your code)Code:str1 = realloc(str1, sizeof(char) * (strlen(str2) + strlen(str1) + 1));
Do you have to write 'sizeof(char)'? Just wondering. Since char is size 1 (or maybe not everywhere?) so it's like multiplication by 1... or is it a good programming style? I have no idea, anybody could explain this a bit?Originally Posted by Kennedy
It's just something Kennedy likes to do. You don't have to multiply by sizeof(char) as the char type will always be the smallest accessible byte.
Will always ask for at least enough memory. Once you start using dynamic memory for things other than strings then you have to start multiplying sizes.Code:char *temp = realloc( str1, strlen(str1) + strlen(str2) + 1 );
> Do you have to write 'sizeof(char)'?
Not if it's a char
But where you had
str1 = malloc(sizeof(char) * 10);
I would have
str1 = malloc( sizeof(*str1) * 10);
which always gives the right answer regardless of what type of pointer str1 is.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.