-2

I am pretty new in C language. I am trying to use strcat, the following example works:

  char s1[20] = "Happy ";
  char s2[15] = "world";
  char s3[40] = "";
  strcat(s3,s1);

Although, i wanted to practise a bit the pointers i have seen earlier, so:

char *s1 = "Happy";
char *s2 = "World";

strcat(*s2,*s1);

produces argument of type "char" is incompatible with parameter of type "const char *. Again, this might be easy, but be as clarifying as possible

iol
  • 27
  • 1
  • 9

3 Answers3

1

In the second example,

strcat(*s2,*s1);

has a type mismatch problem, as *s2 and *s1 are of type char. If you use:

strcat(s2,s1);

It would still be wrong, because s2 points to a string literal, which can't be modified.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • wait.. why s2 can't be modified? – iol May 30 '15 at 19:52
  • @iol Modifying a **string literal** is undefined behavior. – Yu Hao May 30 '15 at 19:53
  • It depends on how the `s2` is declared. If it's a string, as in your second sample, then yes, it can not be modified. But if it's a character array placed on the stack as in your first sample, it you may edit it as you wish. – user35443 May 30 '15 at 19:55
0
strcat(*s2, *s1);

tries to concatenate two 8-bit characters instead of two strings, which is practically impossible. Strcat requires two pointers to strings. What it does is that it finds the end of the first string, and starts moving the second one character by character so the result is a one longer string.

It is declared as

char * strcat (char * destination, const char * source);

and therefore

char s1[20] = "Hello ";
char s2[40] = "World!";
strcat(s2, s1);

is the way to go, because both s2 and s1 are pointers.

However, if you really wanted to play with pointers, you could use

char* strs[2];
strs[0] = "Hello ";
strs[1] = "World!";

char res[20];
strcat(res, *strs);
strcat(res, strs[1]);
user35443
  • 6,309
  • 12
  • 52
  • 75
  • I tried that but i get Access violation writing location 0x0118CC78, – iol May 30 '15 at 19:49
  • @iol On which one? I may have made a mistake. Also, I made a minor edit, have a look. – user35443 May 30 '15 at 19:55
  • ok this works if you initialize char res[20] = " ". Though what i wanted to do is to strcat(strs[0],strs[1]) which does not seem to work – iol May 30 '15 at 20:02
  • Never mind **my fault**, i read the above comments again and it seems like you cannot change the value of string literal so i guess that is why i cannot do an strcat(strs[0],strs[1]); – iol May 30 '15 at 20:03
  • Yes, that's it. Is it OK now? – user35443 May 30 '15 at 20:04
0

The reason why you are having difficulties, is because you are dereferencing the first character in s2 and s1 by using * in front of s2 and s1. Here is why.

Although an array of characters is not considered exactly the same type as a pointer to one or more characters, the array name, let's take s1 as an example, is considered a pointer to the first character in the array s, which is 'H'. Using the name s2 points to the first character in s2, which is 'w'. Therefore if putting an asterisk in front of s2 or s1, results in reading the first character in each of those arrays, respectively.

So, as has been pointed out in another answer, you are trying to concatenate two 8-bit characters, not concatenating one string to the end of the other.

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131