0

I'm trying to append characters from a string into a new string. For the code below:

int main(void)
{

char s1[] = "cat";
char s2[] = "hsklsdksdhkjadsfhjkld";   

strcat(s1, &s2[1]);

printf("s1 is now %s\n", s1);

}

Why is the output catsklsdksdhkjadsfhjkld and not cats? Why is the whole string added, instead of just the 's' located at s2[1]?

Thanks.

2
  • 4
    Because strcat() takes two string arguments, and because &s2[1] isn't a character, its the string "sklsdksdhkjadsfhjkld"; Commented Feb 25, 2015 at 19:14
  • 3
    Not only that, it's UB because s1 isn't large enough to hold any more characters. Commented Feb 25, 2015 at 19:16

4 Answers 4

2

since a char * is only a pointer to the start of a string; C supposes the end of the string is a \0 character. So all characters are added until he meets the \0 character

you suppose &s2[1] points to "s", (which is true), but since it is a char pointer, it points to the whole char array, until the \0 character at the end. Try this for example:

printf("%s\n", &s2[1]);

which will yield:

sklsdksdhkjadsfhjkld

from the reference of strcat:

Concatenate strings Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.

EDIT if you want to add only one (or some) characters, use strncat:

strncat(s1, &s2[1], 1 /*number of chars to append*/);

EDIT2 make sure your char arrays are large enough, as suggested by @PaulR:

char s1[32] = "cat";
Sign up to request clarification or add additional context in comments.

1 Comment

So how would I add just the one character at some index of s2 to s1?
2

Both arguments to strcat are pointers to char objects, and both are assumed to point to (the initial character of) a string.

For example, this:

strcat(s1, &s2[0]);

is equivalent to this:

strcat(s1, s2);

In both cases, the second argument is a pointer to the initial character of a string, which strcat uses to access the entire string up to and including the terminating '\0' null character.

But your program has undefined behavior. By declaring

char s1[] = "cat";

you let the compiler determine the size of s1 based on the initializer. In this case, s1 is an array of 4 chars (including the terminating '\0'). There is no room to append anything to it. Apparently when you ran it, it copied characters into the memory space immediately following s1, which is why it seemed to "work".

A working version of your program is:

#include <stdio.h>
#include <string.h>
int main(void)
{
    char s1[50] = "cat";
    char s2[] = "hsklsdksdhkjadsfhjkld";   

    strcat(s1, &s2[1]);

    printf("s1 is now %s\n", s1);
}

Note the #include directives. These are not optional, even though you might get away with omitting them.

Comments

1

You can use strncat instead of strcat:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char s1[32] = "cat"; // NB: s1 needs to be large enough to hold additional chars !
    char s2[32] = "hsklsdksdhkjadsfhjkld";   

    strncat(s1, &s2[1], 1);

    printf("s1 is now %s\n", s1);

    return 0;
}

LIVE DEMO

Comments

1

Because strcat() takes two string arguments, and because &s2[1] isn't a character, it's the string "sklsdksdhkjadsfhjkld";

So strcat(s1, &s2[1]); concatenates

"cat" + "sklsdksdhkjadsfhjkld"

Giving the result

"catsklsdksdhkjadsfhjkld"

If you want to append a single character you could do this

len = strlen (s1);
s1[len] = s2[1];
s1[len+1] = '\0';       // new string terminator

but you would have to ensure there is enough array (string) space available.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.