how to use strncpy

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Barbara Baby
    New Member
    • Oct 2011
    • 21

    how to use strncpy

    Hi, I have the following codewhich is supposed to use strncpy. Can anyone help, please:

    int i
    for( i = 0; i <= StringCapacity; i++ )
    StringArray[i] = s[i];
    StringArray[i] = '\0';
    StringSize = StringCapacity;

    These lines of code should use strncpy in order to get a much better solution.

    Thank you very much!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    To use strncpy you need to knowthe number of characters to copy.

    strlen will tell you that. So StringCapacity - stren(s) will tell you the number of characters to copy. If negative, copy the entire s otherwise strncpy only the difference.

    Be aware that strncpy does not provide a null terminator unless the entire string is copied. You will need to provide the terminator yourself for the portion that was copied.

    Another solution is to delete the current StringArray and allocate a new StringArray large enough to hold the entire new string. Then you would strcpy and increase your StringCapacity value.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Originally posted by weaknessforcats
      To use strncpy you need to knowthe number of characters to copy.

      strlen will tell you that. So StringCapacity - stren(s) will tell you the number of characters to copy. If negative, copy the entire s otherwise strncpy only the difference.
      You don't need to know the number of characters to copy, you just need to know the size of the destination buffer. Pass the size of the destination buffer as the len parameter and strncpy will copy the source string into the destination buffer, stopping either when the null terminator is copied or the destination buffer is filled up, whichever happens first.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Your starting code is in error. Suppose the s array is smaller than StringCapacity. Your loop will read past the end of s. Accessing beyond the end of an array provokes undefined behavior.

        Comment

        • Barbara Baby
          New Member
          • Oct 2011
          • 21

          #5
          Thank you very much for the advise.
          Here is the complete code:

          MyString::MyStr ing( const char* s )
          {
          int len = strlen(s);
          if( len <= StringCapacity ) //size <= 80
          {
          strcpy( StringArray, s );
          StringSize = len;
          }
          else
          {
          int i;
          for ( i = 0; i <= StringCapacity; i++ );
          StringArray[i] = s[i];
          StringArray[i] = '\0';
          StringSize = len;
          }
          }

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Your code assumes StringArray is defined equivalently to:
            Code:
            char StringArray[StringCapacity+1];
            All of your code can be replaced by three lines if you use strncpy.

            Comment

            • Barbara Baby
              New Member
              • Oct 2011
              • 21

              #7
              I need to use strncpy for these lines:

              else
              {
              int i;
              for ( i = 0; i <= StringCapacity; i++ );
              StringArray[i] = s[i];
              StringArray[i] = '\0';
              StringSize = len;
              }

              How can I chage this?

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                No, you need to replace all of the lines (except for using strlen to set StringSize) with a call to strncpy instead of strcpy.

                How to replace strcpy with strncpy?
                First, consider the differences in their function prototypes:
                Code:
                char *strcpy(char *destination, const char *source);
                char *strncpy(char *destination, const char *source, size_t num);
                Then consider the differences in their functionality:
                • strncpy needs a num argument to specify the maximum number of characters to be copied from source.
                • For strncpy, if the source is longer than the destination (that is, strlen(source)+1 > num), then the destination is not null-terminated.

                Comment

                Working...