strcpy/strcat

Discussion in 'C Programming' started by Seebs, Sep 15, 2009.

  1. Seebs

    Seebs Guest

    On 2009-09-15, Trance <> wrote:
    > I have a quick question related to strcpy/strcat.
    >
    > Let's say I allocated memory for a string named string1. Now, I copy
    > another string, string2, at string1. I allocated more memory for
    > string1 than length of string2 +1. Now, I append another string,
    > string3, to string1. Also, I make sure there's enough memory allocated
    > in the first plate for string1. Namely:
    >
    > strcpy(string1, string2);
    > strcat(string1, string3);
    >
    > Now, if copy yet another string2a into string1, and then concatenate
    > with yet another string string3a, I'm curious what will happen.
    > Namely, I follow the above code with:
    >
    > strcpy(string1, string2a);
    > strcat(string1, string3a);
    >
    > The question is, will the string3a be appended after string 2a, or
    > after string2a and string3? I assume that after doing the strcpy
    > statement above with string1 and string2a, there will be a null
    > character overwriting the first character of original string3.


    I don't know. There will be a null character at the same distance into
    string1 that there was in string2a. If string2a is shorter than string2,
    that will not be the first character of original string3, which will still
    be sitting there untouched.

    > Is this
    > correct? Also, how does strcat behave, will it start append at the
    > first null character of the destination string or after the last
    > nonzero character of the destination string? I would say it's the
    > former, as the initial content of the memory when allocating string1
    > could be non-zero garbage (unless one uses calloc)


    Right you are. The trick is to realize that the "string" is simply
    the set of characters up to the first null -- not the total buffer that
    they are contained in.

    > A more general questions, do strcat and strcpy take care of any memory
    > allocation/deallocation?


    No.

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Sep 15, 2009
    #1
    1. Advertising

  2. Seebs

    Trance Guest

    Hello all,

    I have a quick question related to strcpy/strcat.

    Let's say I allocated memory for a string named string1. Now, I copy
    another string, string2, at string1. I allocated more memory for
    string1 than length of string2 +1. Now, I append another string,
    string3, to string1. Also, I make sure there's enough memory allocated
    in the first plate for string1. Namely:

    strcpy(string1, string2);
    strcat(string1, string3);

    Now, if copy yet another string2a into string1, and then concatenate
    with yet another string string3a, I'm curious what will happen.
    Namely, I follow the above code with:

    strcpy(string1, string2a);
    strcat(string1, string3a);

    The question is, will the string3a be appended after string 2a, or
    after string2a and string3? I assume that after doing the strcpy
    statement above with string1 and string2a, there will be a null
    character overwriting the first character of original string3. Is this
    correct? Also, how does strcat behave, will it start append at the
    first null character of the destination string or after the last
    nonzero character of the destination string? I would say it's the
    former, as the initial content of the memory when allocating string1
    could be non-zero garbage (unless one uses calloc)

    A more general questions, do strcat and strcpy take care of any memory
    allocation/deallocation?

    Thanks very much.
     
    Trance, Sep 15, 2009
    #2
    1. Advertising

  3. Seebs

    Tech07 Guest

    Trance wrote:
    > Hello all,
    >
    > I have a quick question related to strcpy/strcat.
    >
    > Let's say I allocated memory for a string named string1. Now, I copy
    > another string, string2, at string1. I allocated more memory for
    > string1 than length of string2 +1. Now, I append another string,
    > string3, to string1. Also, I make sure there's enough memory allocated
    > in the first plate for string1. Namely:
    >
    > strcpy(string1, string2);
    > strcat(string1, string3);
    >
    > Now, if copy yet another string2a into string1, and then concatenate
    > with yet another string string3a, I'm curious what will happen.
    > Namely, I follow the above code with:
    >
    > strcpy(string1, string2a);
    > strcat(string1, string3a);
    >
    > The question is, will the string3a be appended after string 2a, or
    > after string2a and string3? I assume that after doing the strcpy
    > statement above with string1 and string2a, there will be a null
    > character overwriting the first character of original string3. Is this
    > correct? Also, how does strcat behave, will it start append at the
    > first null character of the destination string or after the last
    > nonzero character of the destination string? I would say it's the
    > former, as the initial content of the memory when allocating string1
    > could be non-zero garbage (unless one uses calloc)
    >
    > A more general questions, do strcat and strcpy take care of any memory
    > allocation/deallocation?
    >
    > Thanks very much.


    Apparently, some unsuspecting "youths" are still pursuing learning C! Is
    that bizarre or what? Oh, it means "youth is wasted on the young", huh.

    I invented roller blades (in my pre-teen years). :)
     
    Tech07, Sep 16, 2009
    #3
    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. JC

    strcpy and strcat problem

    JC, Sep 26, 2003, in forum: C Programming
    Replies:
    23
    Views:
    1,902
    Robert B. Clark
    Sep 29, 2003
  2. Jarmo
    Replies:
    44
    Views:
    1,766
    Irrwahn Grausewitz
    Nov 26, 2003
  3. Pascal Damian

    Difference between strcpy() and strcat()?

    Pascal Damian, Mar 5, 2004, in forum: C Programming
    Replies:
    9
    Views:
    12,750
    Pascal Damian
    Mar 6, 2004
  4. lynology

    Bad File Descriptor Error on strcat/strcpy

    lynology, Aug 18, 2004, in forum: C Programming
    Replies:
    4
    Views:
    506
    Chris Torek
    Aug 18, 2004
  5. Matt
    Replies:
    82
    Views:
    1,784
    Douglas A. Gwyn
    Sep 29, 2004
Loading...

Share This Page