Thread: C Strings

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    1

    Exclamation C Strings

    Hi,

    beginner in ANSI 'c' .

    please can anyone explain me the difference between the strcpy and strncpy ?

    Does the strcpy terminates with null char after the copy operation of the strings.?



    -kanaks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Strcpy copies the string from the source to the destination. It takes no precaution to not write outside the end of the destination. It also places a null char in the destination string after copying the string.
    Strncpy works the same as strcpy but copies a maximum of n characters (as passed by the argument). If the string is longer than n characters, then strncpy copies n characters and does not insert a null char. If the length of the string is less than n characters, then strncpy behaves exactly like strcpy.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Actually the rest of your string buffer will be padded with '\0''s. Be mindful of the fact that if you are needing to deal with large string buffers with small strings within them, this can exhaust memory resources on the box.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by slingerland3g View Post
    Actually the rest of your string buffer will be padded with '\0''s. Be mindful of the fact that if you are needing to deal with large string buffers with small strings within them, this can exhaust memory resources on the box.
    Whoa, I don't think that's true! Unless you specifically zero-out the memory allocated for the string, to the best of my knowledge there's no guarantee of what will be contained in that memory.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    And http://c-faq.com/lib/strncpy.html, which is not exactly like strcpy.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Well to really know the answer, code testing will need to be worked out. From the specification:


    "If the array pointed to by s2 is a string that is shorter than n bytes, null bytes are appended to the copy in the array pointed to by s1, until n bytes in all are written."


    http://www.opengroup.org/pubs/online...h/strncpy.html

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Nice...implementation-defined I guess. Wonder what implementations do not comply with the spec?

    Thanks for setting me straight!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

Tags for this Thread