How is this trivial strncpy implementation

Discussion in 'C Programming' started by lovecreatesbea...@gmail.com, Oct 23, 2007.

  1. Guest

    Do I need to repeat inserting '\0' n - strlen(src) times in case
    strlen(str) less than n. "C: A reference Manual, 5th" mentions this in
    sec 13.3. "K&RC, 2nd" says that insert one '\0' under the same
    condition in sec B.3.

    Thank you for your time.


    /
    *******************************************************************************
    * copy n characters in string src to dst.

    ******************************************************************************/

    char *strncpy(char *dst, const char *src, size_t n)
    {
    char *p = dst;

    while (n-- && (*dst++ = *src++))
    ;
    return p;
    }
     
    , Oct 23, 2007
    #1
    1. Advertising

  2. santosh Guest

    wrote:

    > Do I need to repeat inserting '\0' n - strlen(src) times in case
    > strlen(str) less than n.


    The Standard strncpy does. You can do whatever you want with your
    private variations, but do avoid nameclashes with the Standard library,
    and give a better more descriptive name. The Standard has a lot of
    constraints, not least of which are historical ones, but you can do
    better than cryptic six and seven letter names.

    snip
     
    santosh, Oct 23, 2007
    #2
    1. Advertising

  3. Ben Pfaff Guest

    "" <> writes:

    [about strncpy]
    > Do I need to repeat inserting '\0' n - strlen(src) times in case
    > strlen(str) less than n.


    Yes, strncpy always writes to every byte in the output buffer.
    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
     
    Ben Pfaff, Oct 23, 2007
    #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. Simon
    Replies:
    3
    Views:
    710
    Simon
    Sep 6, 2004
  2. Replies:
    4
    Views:
    1,131
    Dave Moore
    Feb 1, 2005
  3. Re: bizzare strncpy()

    , Jul 9, 2003, in forum: C Programming
    Replies:
    4
    Views:
    375
    Martin Ambuhl
    Jul 9, 2003
  4. lovecreatesbeauty
    Replies:
    17
    Views:
    661
    Keith Thompson
    Jun 16, 2006
  5. baibaichen

    trivial or non-trivial object

    baibaichen, Jan 12, 2006, in forum: C++
    Replies:
    3
    Views:
    912
    osmium
    Jan 12, 2006
Loading...

Share This Page