strncpy definition

This is a discussion on strncpy definition within the C Programming forums, part of the General Programming Boards category; Hi, Can anyone post the source code for strncpy from linux. thanks,...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    strncpy definition

    Hi,

    Can anyone post the source code for strncpy from linux.

    thanks,

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    Implementation is required

    Quote Originally Posted by Salem
    Hi Salem,

    I would like to know how it was implemented.

    my doubt whether the function is taking care of src buffer null character before the specified no of chars.

    for example consider this code

    Code:
    main()
    {
      char src[10], dest[10];
      strcpy(src, "abcdefgh");
    
      src[5] = '\0';
      strncpy(dest, src, 8);
    
      printf(dest);
    
    }
    thanks,

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I would like to know how it was implemented.
    Implementations vary. The best resource is a good reference that either quotes or accurately paraphrases the standard.

    >my doubt whether the function is taking care of src buffer null character before the specified no of chars.
    It sounds like you've discovered the flaw in strncpy. Or rather, the flaw that everyone thinks is a flaw before they discover that strncpy wasn't designed to do what they want to do, but something slightly different, which it does perfectly.
    My best code is written with the delete key.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by abc123
    printf(dest);
    It doesn't really matter what strncpy does here, because your abuse of printf is still going to stop at the null character when it displays the string. By definition, a string ends at the null character. You cannot have strings with embedded null characters. The string ends at the first one. Anything after it is considered to be a new string.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    My worry is all about strncpy not to printf

    Quote Originally Posted by quzah
    It doesn't really matter what strncpy does here, because your abuse of printf is still going to stop at the null character when it displays the string. By definition, a string ends at the null character. You cannot have strings with embedded null characters. The string ends at the first one. Anything after it is considered to be a new string.


    Quzah.
    But, Anyway thanks for your reponce.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't matter about the specific implementation. All you have to do is read its description.
    The function strncpy copies exactly n characters to dest. It first copies up to n characters from src. If there are fewer than n characters in src before the terminating null character, then null characters are written into dest as padding until exactly n characters have been written[/b]. if there are n or more characters in src, then only n characters are copied, and so only a truncated copy of src is transferred to dest. It follows that the copy in dest is terminated with a null by strncpy only if the length of src (not counting the terminating null) is less than n. If the value of n is zero or negative, then calling strncpy has no effect. The value of dest is always returned.
    Quoted from C: A Reference Manual Fifth Edition, page 350.

    Now, since we are dealing with strings, and since by definition, the only way you can tell when a string ends is by finding the first (and thusly only) null character, you have your answer quite clearly. The src string ends at the null character you have placed at src[5]. Anything beyond src[5] is considered a seperate string. Therefore, you can deduce that your destination buffer will contain:

    abcd(null)(null)(null)(null)

    There really isn't any room for doubt here. It's quite clear as to the behavior of the function if you pay attention to the definition of what a string is.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 06:16 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21