Possible Duplicate:
concatenate char array in C
How to concatenate:
char *a="abcd";
char *b="1234";
using the strcat()? And answer to the same when user enters *a and *b?
EDIT missed out this: without using another array.
Possible Duplicate:
concatenate char array in C
How to concatenate:
char *a="abcd";
char *b="1234";
using the strcat()? And answer to the same when user enters *a and *b?
EDIT missed out this: without using another array.
You can't monkey around with the string literals you've already initialized - they're sitting somewhere in memory & can't/shouldn't be rewritten.
If you don't want another statically-defined array but don't mind dynamic allocation, the code below may accomplish what you're looking for:
char *a = "abcd";
char *b = "1234";
char *out;
if((out = (char *)malloc(strlen(a) + strlen(b) + 1)) != NULL)
{
strcpy(out, a);
strcat(out, b);
}
else
{
//you don't have enough memory, handle it
}
If that's still unacceptable, consider a different approach to initializing your string literals.
You need to create a destination location, with enough space to hold all things you want to concat.
char newchar[50];
strcpy(newchar, a); //per the comment
strcat(newchar, b);
How to concatenate without using another array
It is impossible. Both a and b are merely pointers to character string literals residing in ROM. You have not allocated any memory that you can read data to.
What you can do is this:
char a[9] = "abcd";
const char* b = "1234";
strcat(a, b);
Here you allocate 'a' statically as 9 bytes in RAM, just enough to hold the 8 symbols + 1 byte for string termination.
EDIT: The answer below is applicable of OP's question before the edit of the quesiton.
You can't do that. But you can concatenate the strings into a character array. You MUST ensure that the character array has enough room for concatenated string. Here is an example:
char *a = "abcd";
char *b = "1234";
char arr[10] = {0};
strcat(arr, a);
strcat(arr, b);
printf("%s\n", arr);
Note: why arr[] needs to be 0 initialized is a homework for you. :)
The typical way to copy out multiple strings is to create a large enough buffer, copy the first string out and then concatenate the remaining strings one by one. However, string manipulation is often fraught with bugs -- security loopholes -- and you should be extra careful.
I suggest you go through this article: strlcpy and strlcat - consistent, safe, string copy and concatenation
Edit about the OP's edit:
without using another array
You don't have any array to begin with. You have pointers of type char to read-only string literals. You cannot modify either string. Doing so invokes Undefined Behavior. You may however, read and copy these strings to other strings (whose memory you own -- either on stack or on heap -- and thus can modify).
I think the following function should work
void stcat (char *str1, char *str2)
{
int i = 0,len = 0;
while(*(str1+len)!='\0')
len++;
while(*(str2+i)!='\0')
{
*(str1+len) = *(str2+i);
i++;
len++;
}
*(str1+len) = '\0';
}