String Manipulation Functions - strcpy, strncpy

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chikito.chikito@gmail.com

    #1

    String Manipulation Functions - strcpy, strncpy

    1. Can someone tell me the difference between these two functions:

    void strcpy(char *s1, const char *s2)
    {
    while(*s1++ = *s2++)
    ;
    }

    //function prototype of strcpy follows
    char *strcpy(char *s1, const char *s2) // library function

    Both functions are doing the same job.
    Why the later function has to return a char pointer?

    We call both functions in the same manner and we don't sssign the
    return pointer to any other pointer. Correct me if I'm wrong.

    -------------------------
    2. This question is regarding strncpy library function.

    //function prototype of strncpy follows
    char *strncpy(char *s1, const char *s2, size_t n);
    We need to pad '\0' to the end of s1 when n is less than the length of
    s2 plus one.

    Compare strncpy with strncat. The function strncat pad '\0' by itself.
    Why this was left to the programmer in the case of strncpy function.

  • Phlip

    #2
    Re: String Manipulation Functions - strcpy, strncpy

    chikito.chikito wrote:
    1. Can someone tell me the difference between these two functions:
    Firstly, you have a primarily C question, and this newsgroup works best for
    C++.

    C++ inherits the C Standard Library, including these functions, so everyone
    here should know them. You need to learn C++ too, and only use high-level
    systems like std::string in your programs. And if you have a reason to learn
    and use C, you should send future questions like these to news:comp.lang. c .

    Next, these questions are worded like homework. I will answer one in
    grueling detail that might still get you in trouble, and snip the second
    one. Read your tutorial - it's almost as erudite as me.
    void strcpy(char *s1, const char *s2)
    In general, never rename a library function and give it a different return
    value or different contents. Your platform might rely on some detail of the
    original function, and your new function might replace it. If you really
    need an alternate string copy (doubtful), then you should give it a
    different name, so nobody gets confused.

    (And the C Standard reserves all identifiers beginning with str[a-z], so
    follow that rule too.)
    //function prototype of strcpy follows
    char *strcpy(char *s1, const char *s2) // library function
    >
    Both functions are doing the same job.
    Why the later function has to return a char pointer?
    As a convenience to chain calls, such as:

    strcat(strcpy(s lug, label), ": ");

    And note the equivalent in C++ is much clearer:

    slug = label + ": ";

    --
    Phlip



    Comment

    • chikito.chikito@gmail.com

      #3
      Re: String Manipulation Functions - strcpy, strncpy

      On Apr 1, 10:33 pm, "Phlip" <phlip...@yahoo .comwrote:
      chikito.chikito wrote:
      1. Can someone tell me the difference between these two functions:
      >
      Firstly, you have a primarily C question, and this newsgroup works best for
      C++.
      >
      C++ inherits the C Standard Library, including these functions, so everyone
      here should know them. You need to learn C++ too, and only use high-level
      systems like std::string in your programs. And if you have a reason to learn
      and use C, you should send future questions like these to news:comp.lang. c .
      >
      Next, these questions are worded like homework. I will answer one in
      grueling detail that might still get you in trouble, and snip the second
      one. Read your tutorial - it's almost as erudite as me.
      >
      void strcpy(char *s1, const char *s2)
      >
      In general, never rename a library function and give it a different return
      value or different contents. Your platform might rely on some detail of the
      original function, and your new function might replace it. If you really
      need an alternate string copy (doubtful), then you should give it a
      different name, so nobody gets confused.
      >
      (And the C Standard reserves all identifiers beginning with str[a-z], so
      follow that rule too.)
      >
      //function prototype of strcpy follows
      char *strcpy(char *s1, const char *s2) // library function
      >
      Both functions are doing the same job.
      Why the later function has to return a char pointer?
      >
      As a convenience to chain calls, such as:
      >
      strcat(strcpy(s lug, label), ": ");
      >
      And note the equivalent in C++ is much clearer:
      >
      slug = label + ": ";
      >
      --
      Phlip
      http://flea.sourceforge.net/PiglegToo_1.html

      Philip,

      I understand the reasons now. Thanks.

      Comment

      • Jim Langston

        #4
        Re: String Manipulation Functions - strcpy, strncpy


        <chikito.chikit o@gmail.comwrot e in message
        news:1175437466 .167773.30490@b 75g2000hsg.goog legroups.com...
        1. Can someone tell me the difference between these two functions:
        >
        void strcpy(char *s1, const char *s2)
        {
        while(*s1++ = *s2++)
        ;
        }
        >
        //function prototype of strcpy follows
        char *strcpy(char *s1, const char *s2) // library function
        >
        Both functions are doing the same job.
        Why the later function has to return a char pointer?
        >
        We call both functions in the same manner and we don't sssign the
        return pointer to any other pointer. Correct me if I'm wrong.
        You can use the returned pointer if you need to. Consider something like:

        printf( "%s", strcat( str1, str2 ) );
        or other fuctions that accept a string.
        -------------------------
        2. This question is regarding strncpy library function.
        >
        //function prototype of strncpy follows
        char *strncpy(char *s1, const char *s2, size_t n);
        We need to pad '\0' to the end of s1 when n is less than the length of
        s2 plus one.
        >
        Compare strncpy with strncat. The function strncat pad '\0' by itself.
        Why this was left to the programmer in the case of strncpy function.
        strncpy basically does the exact same thing memcpy does. It copies n bytes
        starting at sr2 into sr1. Those bytes can be binary, which can include a
        null terminator (\0). Also, you may want to copy into the middle of a
        string and appending an \0 would mess it up.

        char Text[100];
        strcpy( Text, "The blue pen is here." );
        char Color[100];
        strcpy( Color, "red " );
        strncpy( Text + 3, Color );

        This would replace "blue" with "red "
        If strncpy added a null terminator, if you printed it would output:
        "The red "
        because of the null terminator.

        With strcat, however, you are adding a string to the end of another string,
        so there will be text at the end of the first string anyway.


        Comment

        • chikito.chikito@gmail.com

          #5
          Re: String Manipulation Functions - strcpy, strncpy

          On Apr 1, 10:59 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
          <chikito.chik.. .@gmail.comwrot e in message
          >
          news:1175437466 .167773.30490@b 75g2000hsg.goog legroups.com...
          >
          >
          >
          1. Can someone tell me the difference between these two functions:
          >
          void strcpy(char *s1, const char *s2)
          {
          while(*s1++ = *s2++)
          ;
          }
          >
          //function prototype of strcpy follows
          char *strcpy(char *s1, const char *s2) // library function
          >
          Both functions are doing the same job.
          Why the later function has to return a char pointer?
          >
          We call both functions in the same manner and we don't sssign the
          return pointer to any other pointer. Correct me if I'm wrong.
          >
          You can use the returned pointer if you need to. Consider something like:
          >
          printf( "%s", strcat( str1, str2 ) );
          or other fuctions that accept a string.
          >
          -------------------------
          2. This question is regarding strncpy library function.
          >
          //function prototype of strncpy follows
          char *strncpy(char *s1, const char *s2, size_t n);
          We need to pad '\0' to the end of s1 when n is less than the length of
          s2 plus one.
          >
          Compare strncpy with strncat. The function strncat pad '\0' by itself.
          Why this was left to the programmer in the case of strncpy function.
          >
          strncpy basically does the exact same thing memcpy does. It copies n bytes
          starting at sr2 into sr1. Those bytes can be binary, which can include a
          null terminator (\0). Also, you may want to copy into the middle of a
          string and appending an \0 would mess it up.
          >
          char Text[100];
          strcpy( Text, "The blue pen is here." );
          char Color[100];
          strcpy( Color, "red " );
          strncpy( Text + 3, Color );
          >
          This would replace "blue" with "red "
          If strncpy added a null terminator, if you printed it would output:
          "The red "
          because of the null terminator.
          >
          With strcat, however, you are adding a string to the end of another string,
          so there will be text at the end of the first string anyway.

          ..... we may want to copy into the middle of a
          string and appending an \0 would mess it up. So, it is better that the
          programmer appends the \0 wherever necessary.

          Thanks Langston

          Comment

          Working...