0

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.

Community
  • 1
  • 1
  • "And answer to the same when user enters *a and *b?" - What purpose does this serve? What other user inputs can be expected? Why? – ArjunShankar May 07 '12 at 10:10
  • The only tricky part is to pre-allocate the target (e.g. char str[20]="";) and call strcat twice (strcat(str,a); strcat(str,b);) – Ofir May 07 '12 at 10:10
  • Do you actually have dynamically determined strings in your program, or do you really just have literal string constants? In the latter case, you can do most of the work at compile time using arrays and `sizeof`. – Kerrek SB May 07 '12 at 10:22

6 Answers6

3

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.

laughingcoyote
  • 259
  • 2
  • 9
1

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);
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
1

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

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. :)

Donotalo
  • 12,748
  • 25
  • 83
  • 121
0

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).

dirkgently
  • 108,024
  • 16
  • 131
  • 187
-2

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';
}
provokoe
  • 178
  • 2
  • 10
  • 1
    This is what even i came up with. But it results in a runtime error. –  May 07 '12 at 11:26
  • You can't use this, since there are no arrays allocated, just constant string literals residing in ROM. – Lundin May 07 '12 at 12:41