strcpy and strcat problem

Discussion in 'C Programming' started by JC, Sep 26, 2003.

  1. JC

    JC Guest

    hi,

    i want to combine two string together.. and put in to another string. how
    can i do . i try myself.. with the follow code. but seem can't get the
    result i want.. i want to get the result with "c is abcd" .
    #include <stdio.h>
    #include <string.h>
    void main() {
    char a[2]="ab";
    char b[2]="cd";
    char c[4]=" \0";
    strcpy(c,a);
    strcat(c,b);
    printf("a is %s\n",a);
    printf("b is %s\n",b);
    printf("c is %s\n",c);
    }

    i only get this result

    a is b?
    b is b?
    c is ab?b?

    what problem to my coding? anything wrong?
    please help!!.

    Thanks

    JC

    ps. if i use strcpy(c,"ab"); and strcat(c,"cd"); i can get the result.."c is
    abcd"
     
    JC, Sep 26, 2003
    #1
    1. Advertising

  2. JC

    Mike Wahler Guest

    "JC" <> wrote in message news:bl1pas$-cable.com...
    > hi,
    >
    > i want to combine two string together.. and put in to another string. how
    > can i do . i try myself.. with the follow code. but seem can't get the
    > result i want.. i want to get the result with "c is abcd" .
    > #include <stdio.h>
    > #include <string.h>
    > void main() {


    int main() {

    > char a[2]="ab";


    char a[] = "ab";

    > char b[2]="cd";


    char b[] = "cd";

    > char c[4]=" \0";


    char c[sizeof a + sizeof b + 1] = {0};

    > strcpy(c,a);
    > strcat(c,b);
    > printf("a is %s\n",a);
    > printf("b is %s\n",b);
    > printf("c is %s\n",c);
    > }
    >
    > i only get this result
    >
    > a is b?
    > b is b?
    > c is ab?b?
    >
    > what problem to my coding?


    Your arrays did not provide room for the string terminators.

    >anything wrong?


    Yes. Undefined behavior.

    -Mike

    > please help!!.
    >
    > Thanks
    >
    > JC
    >
    > ps. if i use strcpy(c,"ab");


    OK so far. The literal contains a terminator.

    >and strcat(c,"cd"); i can get the result.."c is


    Not OK, the terminator from the literal "cd" will
    be written to c[4] -- out of bounds -- undefined
    behavior.

    > abcd"


    Only by accident.

    -Mike
     
    Mike Wahler, Sep 26, 2003
    #2
    1. Advertising

  3. "JC" <> wrote:

    >hi,
    >
    >i want to combine two string together.. and put in to another string. how
    >can i do . i try myself.. with the follow code. but seem can't get the
    >result i want.. i want to get the result with "c is abcd" .


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


    >void main() {

    undefined behaviour, use:

    int main( void )
    {

    >char a[2]="ab";
    >char b[2]="cd";
    >char c[4]=" \0";

    Undefined behaviour, you reserved to less memory for a, b and c.
    Remember: "xy" is a string literal consisting of _three_ characters,
    'x', 'y' and the implicit terminating '\0'. The explicit use of '\0'
    in the initializer for c is unnecessary.

    char a[3]="ab";
    char b[3]="cd";
    char c[5]=" ";


    >strcpy(c,a);
    >strcat(c,b);
    >printf("a is %s\n",a);
    >printf("b is %s\n",b);
    >printf("c is %s\n",c);
    >}
    >
    >i only get this result
    >
    >a is b?
    >b is b?
    >c is ab?b?
    >
    >what problem to my coding? anything wrong?
    >please help!!.
    >

    See above.

    >ps. if i use strcpy(c,"ab"); and strcat(c,"cd"); i can get the result.."c is
    >abcd"
    >

    This worked only by chance, see above.

    Regards

    Irrwahn
    --
    ERROR 103: Dead mouse in hard drive.
     
    Irrwahn Grausewitz, Sep 26, 2003
    #3
  4. On Sat, 27 Sep 2003 00:37:31 +0800, "JC" <> wrote:

    >i want to combine two string together.. and put in to another string. how
    >can i do . i try myself.. with the follow code. but seem can't get the
    >result i want.. i want to get the result with "c is abcd" .
    >#include <stdio.h>
    >#include <string.h>
    >void main() {


    int main(void)

    main returns an int.

    >char a[2]="ab";


    The string "ab" actually has three chars ('a', 'b' and the terminating NUL
    character '\0'), but you've only allocated two in array a[].

    Change this to

    char a[3]="ab";

    or
    char a[] = "ab";

    In the latter case, the compiler will allocate sufficient storage (3) for
    you.

    >char b[2]="cd";
    >char c[4]=" \0";


    Ditto.

    Note that c[] may hold only three characters plus the terminating NUL, so
    you'll want to increase the allocated space in preparation for your string
    concatenation below:

    char c[5] = '\0'; /* 4 chars + terminating NUL */

    >strcpy(c,a);
    >strcat(c,b);
    >printf("a is %s\n",a);
    >printf("b is %s\n",b);
    >printf("c is %s\n",c);


    return 0;
    >}


    >ps. if i use strcpy(c,"ab"); and strcat(c,"cd"); i can get the result.."c is
    >abcd"


    Buffer overrun in c.

    Just because something is UB, doesn't mean that the result of that UB *has*
    to be counter to one's expectations. :)

    --
    Robert B. Clark (email ROT13'ed)
    Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
     
    Robert B. Clark, Sep 26, 2003
    #4
  5. JC

    CBFalconer Guest

    JC wrote:
    >
    > i want to combine two string together.. and put in to another
    > string. how can i do . i try myself.. with the follow code. but
    > seem can't get the result i want.. i want to get the result with
    > "c is abcd" .
    >
    > #include <stdio.h>
    > #include <string.h>
    > void main() {
    > char a[2]="ab";


    This is not a string. It has no trailing '\0', which it would if
    the array size was 3.

    > char b[2]="cd";


    same here.

    > char c[4]=" \0";


    I'm not sure what this is, but it isn't a string either.

    > strcpy(c,a);
    > strcat(c,b);


    You have to have strings to use strcpy and strcat.

    .... snip ...
    >
    > what problem to my coding? anything wrong?


    Yes.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Sep 26, 2003
    #5
  6. On Sat, 27 Sep 2003, JC wrote:

    > hi,
    >
    > i want to combine two string together.. and put in to another string. how
    > can i do . i try myself.. with the follow code. but seem can't get the
    > result i want.. i want to get the result with "c is abcd" .
    > #include <stdio.h>
    > #include <string.h>
    > void main() {


    The main function should be:

    int main(void)

    > char a[2]="ab";
    > char b[2]="cd";


    The string "ab" is actually the array { 'a', 'b', '\0' }; When written
    this way you should be able to see there are 3 elements in the array. so
    change these to:

    char a[] = "ab";
    char b[] = "cd";

    > char c[4]=" \0";


    Since you are copying a[] and b[] to c[] you want to end up with the array
    { 'a', 'b', 'c', 'd', '\0' }; This has five elements; not four. So just
    use:

    char c[5];

    > strcpy(c,a);
    > strcat(c,b);


    This would work. You can also do:

    sprintf(c, "%s%s", a, b);

    > printf("a is %s\n",a);
    > printf("b is %s\n",b);
    > printf("c is %s\n",c);


    return 0;

    > }
    >
    > i only get this result
    >
    > a is b?
    > b is b?
    > c is ab?b?
    >
    > what problem to my coding? anything wrong?
    > please help!!.
    >
    > Thanks
    >
    > JC
    >
    > ps. if i use strcpy(c,"ab"); and strcat(c,"cd"); i can get the result.."c is
    > abcd"


    Just luck.

    --
    Send e-mail to: darrell at cs dot toronto dot edu
    Don't send e-mail to
     
    Darrell Grainger, Sep 26, 2003
    #6
  7. CBFalconer <> wrote:

    >JC wrote:

    <SNIP>
    >> char c[4]=" \0";

    >
    >I'm not sure what this is, but it isn't a string either.
    >

    To me it looks like it is a string consisting of three blanks
    followed by a null character, inititialized invoking undefined
    behaviour by writing beyond array bounds.

    <SNIP>

    Irrwahn
    --
    ERROR 103: Dead mouse in hard drive.
     
    Irrwahn Grausewitz, Sep 26, 2003
    #7
  8. JC

    Mike Wahler Guest

    "CBFalconer" <> wrote in message
    news:...

    > > char c[4]=" \0";

    >
    > I'm not sure what this is, but it isn't a string either.


    Sure it is. Three spaces followed by a terminator.

    Same as

    char c[] = " ";

    -Mike
     
    Mike Wahler, Sep 26, 2003
    #8
  9. JC wrote:

    > hi,
    >
    > i want to combine two string together.. and put in to another string. how
    > can i do . i try myself.. with the follow code. but seem can't get the
    > result i want.. i want to get the result with "c is abcd" .
    > #include <stdio.h>
    > #include <string.h>
    > void main() {
    > char a[2]="ab";
    > char b[2]="cd";
    > char c[4]=" \0";
    > strcpy(c,a);
    > strcat(c,b);
    > printf("a is %s\n",a);
    > printf("b is %s\n",b);
    > printf("c is %s\n",c);
    > }
    >
    > i only get this result
    >
    > a is b?
    > b is b?
    > c is ab?b?
    >
    > what problem to my coding? anything wrong?
    > please help!!.
    >
    > Thanks
    >
    > JC
    >
    > ps. if i use strcpy(c,"ab"); and strcat(c,"cd"); i can get the result.."c is
    > abcd"




    JC,


    Thank you for your question.

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

    int main()
    {
    char a[] = "ab";
    char b[] = "cd";

    char c[5];

    strcpy(c, a); /* Instead of these two lines, you may write */
    strcat(c, b); /* strcpy(c, strcat(a, b)); */

    printf("%s\n", c);

    return 0;
    }

    --Steve
     
    Steve Zimmerman, Sep 27, 2003
    #9
  10. On Sat, 27 Sep 2003, Steve Zimmerman wrote:
    >
    > JC,
    >
    > Thank you for your question.
    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main()
    > {
    > char a[] = "ab";
    > char b[] = "cd";
    >
    > char c[5];
    >
    > strcpy(c, a); /* Instead of these two lines, you may write */
    > strcat(c, b); /* strcpy(c, strcat(a, b)); */


    I seem to recall having said this before, but:

    STOP STOP STOP STOP STOP STOP STOP STOP STOP!!

    Do *NOT* give newbies incorrect information!
    It's bad!
    Don't do it!
    Resist the temptation!
    Pick up a book and learn how to answer questions correctly!
    Thank you!


    [The "Instead of..." comment is utterly wrong; the code
    suggested won't even "work" on typical x86 implementations --
    so it's fairly obvious that the comment was the product of
    either willful ignorance or maliciousness.]

    -Arthur
     
    Arthur J. O'Dwyer, Sep 27, 2003
    #10
  11. Arthur J. O'Dwyer wrote:

    >
    > On Sat, 27 Sep 2003, Steve Zimmerman wrote:
    >>
    >> JC,
    >>
    >> Thank you for your question.
    >>
    >> #include <stdio.h>
    >> #include <string.h>
    >>
    >> int main()
    >> {
    >> char a[] = "ab";
    >> char b[] = "cd";
    >>
    >> char c[5];
    >>
    >> strcpy(c, a); /* Instead of these two lines, you may write */
    >> strcat(c, b); /* strcpy(c, strcat(a, b)); */

    >
    > I seem to recall having said this before, but:
    >
    > STOP STOP STOP STOP STOP STOP STOP STOP STOP!!
    >
    > Do *NOT* give newbies incorrect information!
    > It's bad!
    > Don't do it!
    > Resist the temptation!
    > Pick up a book and learn how to answer questions correctly!
    > Thank you!


    It appears that you're wasting your time, Arthur. Just about every Zimmerman
    article I read is flawed in some major respect, and he seems to ignore
    corrections. You might as well just address the corrections directly to the
    OP...

    > [The "Instead of..." comment is utterly wrong; the code
    > suggested won't even "work" on typical x86 implementations --
    > so it's fairly obvious that the comment was the product of
    > either willful ignorance or maliciousness.]


    ....like this, only perhaps in more detail.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Sep 27, 2003
    #11
  12. Irrwahn Grausewitz wrote:

    > CBFalconer <> wrote:
    >
    >>JC wrote:

    > <SNIP>
    >>> char c[4]=" \0";

    >>
    >>I'm not sure what this is, but it isn't a string either.
    >>

    > To me it looks like it is a string consisting of three blanks
    > followed by a null character, inititialized invoking undefined
    > behaviour by writing beyond array bounds.


    No, the string literal comprises "whatever is in quotes" plus a null
    terminating character. If "whatever is in quotes" comes to exactly the
    specified array size, the terminator is ignored. In this case, it matters
    little, since '\0' is included in the "whatever is in quotes" bit.

    In the following case:

    char d[4] = "abcd";

    the code is legal, d[0] is 'a', ... d[3] is 'd', and there is no null
    terminator, so d is not a string.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Sep 27, 2003
    #12
  13. JC

    JC Guest

    Thanks for help!!

    JC

    "Steve Zimmerman" <> wrote in message
    news:...
    > JC wrote:
    >
    > > hi,
    > >
    > > i want to combine two string together.. and put in to another string.

    how
    > > can i do . i try myself.. with the follow code. but seem can't get the
    > > result i want.. i want to get the result with "c is abcd" .
    > > #include <stdio.h>
    > > #include <string.h>
    > > void main() {
    > > char a[2]="ab";
    > > char b[2]="cd";
    > > char c[4]=" \0";
    > > strcpy(c,a);
    > > strcat(c,b);
    > > printf("a is %s\n",a);
    > > printf("b is %s\n",b);
    > > printf("c is %s\n",c);
    > > }
    > >
    > > i only get this result
    > >
    > > a is b?
    > > b is b?
    > > c is ab?b?
    > >
    > > what problem to my coding? anything wrong?
    > > please help!!.
    > >
    > > Thanks
    > >
    > > JC
    > >
    > > ps. if i use strcpy(c,"ab"); and strcat(c,"cd"); i can get the

    result.."c is
    > > abcd"

    >
    >
    >
    > JC,
    >
    >
    > Thank you for your question.
    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main()
    > {
    > char a[] = "ab";
    > char b[] = "cd";
    >
    > char c[5];
    >
    > strcpy(c, a); /* Instead of these two lines, you may write */
    > strcat(c, b); /* strcpy(c, strcat(a, b)); */
    >
    > printf("%s\n", c);
    >
    > return 0;
    > }
    >
    > --Steve
    >
     
    JC, Sep 27, 2003
    #13
  14. Richard Heathfield <> wrote:

    >Irrwahn Grausewitz wrote:
    >
    >> CBFalconer <> wrote:
    >>
    >>>JC wrote:

    >> <SNIP>
    >>>> char c[4]=" \0";
    >>>
    >>>I'm not sure what this is, but it isn't a string either.
    >>>

    >> To me it looks like it is a string consisting of three blanks
    >> followed by a null character, inititialized invoking undefined
    >> behaviour by writing beyond array bounds.

    >
    >No, the string literal comprises "whatever is in quotes" plus a null
    >terminating character. If "whatever is in quotes" comes to exactly the
    >specified array size, the terminator is ignored. In this case, it matters
    >little, since '\0' is included in the "whatever is in quotes" bit.


    Right, of course, but I was referring to c, not the string literal it
    was initialized with. I did not express myself clearly though.

    <SNIP>

    --
    Computer: a million morons working at the speed of light.
     
    Irrwahn Grausewitz, Sep 27, 2003
    #14
  15. JC

    JC Guest

    sorry . i got one more problem
    i got a string with 4 char. i want to put that in a string with 26 char. how
    can i fill space on the remain char.. ??
    any sample method?

    please help!

    thanks
    Jack
     
    JC, Sep 27, 2003
    #15
  16. Irrwahn Grausewitz wrote:

    > Richard Heathfield <> wrote:
    >
    >>Irrwahn Grausewitz wrote:
    >>
    >>> CBFalconer <> wrote:
    >>>
    >>>>JC wrote:
    >>> <SNIP>
    >>>>> char c[4]=" \0";
    >>>>
    >>>>I'm not sure what this is, but it isn't a string either.
    >>>>
    >>> To me it looks like it is a string consisting of three blanks
    >>> followed by a null character, inititialized invoking undefined
    >>> behaviour by writing beyond array bounds.

    >>
    >>No, the string literal comprises "whatever is in quotes" plus a null
    >>terminating character. If "whatever is in quotes" comes to exactly the
    >>specified array size, the terminator is ignored. In this case, it matters
    >>little, since '\0' is included in the "whatever is in quotes" bit.

    >
    > Right, of course, but I was referring to c, not the string literal it
    > was initialized with. I did not express myself clearly though.


    Perhaps /I/ didn't express myself clearly enough. The initialisation does
    /not/ invoke undefined behaviour. The end contents of c are ' ', ' ', ' ',
    '\0', and the fifth character in " \0" is /not/ used for initialisation
    in this special case.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Sep 27, 2003
    #16
  17. JC

    Mike Wahler Guest

    "JC" <> wrote in message news:bl3inb$-cable.com...
    > sorry . i got one more problem
    > i got a string with 4 char. i want to put that in a string with 26 char.

    how
    > can i fill space on the remain char.. ??


    Fill with what? Space characters? Zero characters?

    The following will let you pass the 'filler' character
    as a function argument.

    > any sample method?


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

    char *copyandpad(char *dest, const char *src,
    size_t dest_len, char fillchar)
    {
    size_t len = strlen(src);
    strcpy(dest, src);
    memset(dest + len, fillchar, dest_len - len - 1);
    return dest;
    }
    int main()
    {
    char s1[] = "1234";
    char s2[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char fill = 'X'; /* adjust this value to your needs */

    printf("Before: %s\n", s2);
    copyandpad(s2, s1, sizeof s2, fill);
    printf("After : %s\n", s2);
    return 0;
    }


    Output:

    Before: ABCDEFGHIJKLMNOPQRSTUVWXYZ
    After : 1234XXXXXXXXXXXXXXXXXXXXXX

    HTH,
    -Mike
     
    Mike Wahler, Sep 27, 2003
    #17
  18. Richard Heathfield <> wrote:

    >Irrwahn Grausewitz wrote:
    >
    >> Richard Heathfield <> wrote:
    >>
    >>>Irrwahn Grausewitz wrote:
    >>>
    >>>> CBFalconer <> wrote:
    >>>>
    >>>>>JC wrote:
    >>>> <SNIP>
    >>>>>> char c[4]=" \0";
    >>>>>
    >>>>>I'm not sure what this is, but it isn't a string either.
    >>>>>
    >>>> To me it looks like it is a string consisting of three blanks
    >>>> followed by a null character, inititialized invoking undefined
    >>>> behaviour by writing beyond array bounds.
    >>>
    >>>No, the string literal comprises "whatever is in quotes" plus a null
    >>>terminating character. If "whatever is in quotes" comes to exactly the
    >>>specified array size, the terminator is ignored. In this case, it matters
    >>>little, since '\0' is included in the "whatever is in quotes" bit.

    >>
    >> Right, of course, but I was referring to c, not the string literal it
    >> was initialized with. I did not express myself clearly though.

    >
    >Perhaps /I/ didn't express myself clearly enough. The initialisation does
    >/not/ invoke undefined behaviour. The end contents of c are ' ', ' ', ' ',
    >'\0', and the fifth character in " \0" is /not/ used for initialisation
    >in this special case.


    <slaps forehead> Most certainly I didn't engage my brain before reading.
    :)

    Irrwahn
    --
    Great minds run in great circles.
     
    Irrwahn Grausewitz, Sep 27, 2003
    #18
  19. JC

    goose Guest

    "JC" <> wrote in message news:<bl3inb$-cable.com>...
    > sorry . i got one more problem
    > i got a string with 4 char. i want to put that in a string with 26 char. how
    > can i fill space on the remain char.. ??
    > any sample method?
    >


    put it at the beginning of the 26 char string ?

    1. use memset() to set all 26 elements of your
    destination string to the space character.
    2. use assignment to set the last element of
    your destination array to 0 (zero) so that it
    is a valid string.
    3. use memcpy() to copy 4 chars from the 4 element
    string to the destination string.

    check your textbook to find out what memset and memcpy do.

    goose,
    hth
     
    goose, Sep 27, 2003
    #19
  20. On Sat, 27 Sep 2003 16:56:55 +0800, "JC" <> wrote:

    >sorry . i got one more problem
    >i got a string with 4 char. i want to put that in a string with 26 char. how
    >can i fill space on the remain char.. ??
    >any sample method?
    >

    When you say 4 char, does that include the terminating '\0' or not?

    If you strcpy a string with 4 characters to an array of 26 char, the 4
    (or 5) characters of the string (including the '\0') will be copied to
    the first 4 (or 5) elements of the array. The remainder of the array
    is untouched. If you pass this array to any string function or use it
    in any string context, only the 4 (or 5) char in the string
    participate. There is no need to fill in the "extraneous" portion of
    the array.

    If you want the resulting string to actually be 26 char long
    (including the '\0') and you want to pad the right end of the string
    with some char, you could do something like

    char c = '?'; /*whatever pad character you like*/
    size_t len;
    len = strlen(array);
    if (len < 25){
    memset(array+len, c, 25-len);
    array[25] = '\0';
    }


    <<Remove the del for email>>
     
    Barry Schwarz, Sep 27, 2003
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pascal Damian

    Difference between strcpy() and strcat()?

    Pascal Damian, Mar 5, 2004, in forum: C Programming
    Replies:
    9
    Views:
    12,733
    Pascal Damian
    Mar 6, 2004
  2. Matt
    Replies:
    82
    Views:
    1,780
    Douglas A. Gwyn
    Sep 29, 2004
  3. strcpy and strcat's return type

    , Jul 26, 2005, in forum: C Programming
    Replies:
    14
    Views:
    805
    Kenneth Brody
    Aug 22, 2005
  4. Replies:
    24
    Views:
    1,183
    Greg Comeau
    Oct 30, 2005
  5. =?ISO-8859-1?Q?Une_b=E9vue?=

    [newbie] strcpy, strtok and strcat problem...

    =?ISO-8859-1?Q?Une_b=E9vue?=, Aug 31, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,242
    =?ISO-8859-1?Q?Une_b=E9vue?=
    Sep 1, 2006
Loading...

Share This Page