Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #17706

Re: strncpy and 'n'

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: strncpy and 'n'
Date 2012-02-20 14:46 -0800
Organization None to speak of
Message-ID <lnmx8dcedm.fsf@nuthaus.mib.org> (permalink)
References <58d7c42a-7fd9-4507-bbe6-88941700b88a@k6g2000vbz.googlegroups.com> <lnvcn6fyi2.fsf@nuthaus.mib.org> <jhrkl9$g1h$1@reader1.panix.com> <lnboouef7m.fsf@nuthaus.mib.org> <jhudnl$gkf$1@reader1.panix.com>

Show all headers | View raw


jgk@panix.com (Joe keane) writes:
> In article <lnboouef7m.fsf@nuthaus.mib.org>,
> Keith Thompson  <kst-u@mib.org> wrote:
>>strncpy *looks* like it should be to strcpy as strncat is to strcat,
>>but it isn't.
>
> Well there is a number of options.
>
> a) should it make sure there is a zero terminator
> b) maybe you want to clear everything that isn't copied
> c) does it handle overlapping copies
>
> Do i think it's slightly stupid that they don't match?  A little bit.
> But one can imagine that good choices were made, and that consistency is
> sometimes negative.

The point is that strncpy is a very different function from strcpy.
It is not intended to work with a *string* in the target array;
it works with a specialized data structure (used to store file
names in very early Unix systems).

> Why does 'puts' add a newline, and 'fputs' doesn't?
> Why does 'fgets' take a size argument, and 'gets' doesn't?
> Why can i use the same format for float/double in 'fprintf', but not in
> 'fscanf'?
> Why is it that multiply long and long gives long, but short and short
> gives int?
> Why can i use a bitfield in a struct, but not as a local variable?
> Why is it that 'int' means signed, but 'char' can be either one?

There are answers for most of these questions.  For others,
it's certainly true that the C standard library is not entirelyi
consistent.  But I don't think any of them are particularly relevant
to strncpy.

I think you're understating the differences between strcpy and
strncpy.  The strncpy function is radically different from strcpy,
and there are very few legitimate uses for it.  On the other hand,
the deceptive name has led many C programmers to use it incorrectly,
and I strongly suspect that it's  used incorrectly far more often
than it's used correctly.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

strncpy and 'n' nroberts <roberts.noah@gmail.com> - 2012-02-16 09:05 -0800
  Re: strncpy and 'n' Anders Wegge Keller <wegge@wegge.dk> - 2012-02-16 18:57 +0100
    Re: strncpy and 'n' "A. K." <akk@nospam.org> - 2012-02-16 19:27 +0100
      Re: strncpy and 'n' Anders Wegge Keller <wegge@wegge.dk> - 2012-02-16 19:34 +0100
        Re: strncpy and 'n' "A. K." <akk@nospam.org> - 2012-02-16 20:58 +0100
  Re: strncpy and 'n' Keith Thompson <kst-u@mib.org> - 2012-02-16 10:01 -0800
    Re: strncpy and 'n' Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-16 11:10 -0800
      Re: strncpy and 'n' Keith Thompson <kst-u@mib.org> - 2012-02-16 14:20 -0800
        Re: strncpy and 'n' Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-17 04:19 -0800
          Re: strncpy and 'n' James Kuyper <jameskuyper@verizon.net> - 2012-02-17 10:04 -0500
            Re: strncpy and 'n' Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-17 10:27 -0800
            Re: strncpy and 'n' Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-17 11:22 -0800
              Re: strncpy and 'n' James Kuyper <jameskuyper@verizon.net> - 2012-02-17 14:40 -0500
                Re: strncpy and 'n' Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-18 02:17 -0800
    Re: strncpy and 'n' jgk@panix.com (Joe keane) - 2012-02-19 20:04 +0000
      Re: strncpy and 'n' Keith Thompson <kst-u@mib.org> - 2012-02-19 12:33 -0800
        Re: strncpy and 'n' jgk@panix.com (Joe keane) - 2012-02-20 21:25 +0000
          Re: strncpy and 'n' Keith Thompson <kst-u@mib.org> - 2012-02-20 14:46 -0800
            Re: strncpy and 'n' Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-02-23 23:13 +0000
              Re: strncpy and 'n' Keith Thompson <kst-u@mib.org> - 2012-02-23 16:20 -0800
              Re: strncpy and 'n' Philip Lantz <prl@canterey.us> - 2012-02-25 00:26 -0800
              Re: strncpy and 'n' pacman@kosh.dhis.org (Alan Curry) - 2012-02-26 01:34 +0000
                Re: strncpy and 'n' Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-02-28 22:46 +0000
                Re: strncpy and 'n' jgk@panix.com (Joe keane) - 2012-03-01 00:03 +0000
                Re: strncpy and 'n' Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-03-01 12:03 +0000
          Re: strncpy and 'n' James Kuyper <jameskuyper@verizon.net> - 2012-02-20 17:51 -0500
        Re: strncpy and 'n' Shao Miller <sha0.miller@gmail.com> - 2012-02-20 18:14 -0500
          Re: strncpy and 'n' pete <pfiland@mindspring.com> - 2012-02-20 23:16 -0500
            Re: strncpy and 'n' Shao Miller <sha0.miller@gmail.com> - 2012-02-25 01:51 -0500
              Re: strncpy and 'n' pete <pfiland@mindspring.com> - 2012-02-25 10:34 -0500
        Re: strncpy and 'n' Rich Webb <bbew.ar@mapson.nozirev.ten> - 2012-02-20 19:09 -0500
          Re: strncpy and 'n' Keith Thompson <kst-u@mib.org> - 2012-02-20 16:46 -0800
            Re: strncpy and 'n' Rich Webb <bbew.ar@mapson.nozirev.ten> - 2012-02-20 20:48 -0500
          Re: strncpy and 'n' Shao Miller <sha0.miller@gmail.com> - 2012-02-20 19:46 -0500
        Re: strncpy and 'n' jgk@panix.com (Joe keane) - 2012-02-21 21:46 +0000
          Re: strncpy and 'n' James Kuyper <jameskuyper@verizon.net> - 2012-02-21 17:15 -0500
          Re: strncpy and 'n' Keith Thompson <kst-u@mib.org> - 2012-02-21 14:29 -0800
            Re: strncpy and 'n' Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-22 01:15 -0800
            Re: strncpy and 'n' jgk@panix.com (Joe keane) - 2012-02-23 00:02 +0000
              Re: strncpy and 'n' Keith Thompson <kst-u@mib.org> - 2012-02-22 17:02 -0800
  Re: strncpy and 'n' Markus Schaub <usenet@markus-schaub.de> - 2012-02-16 18:53 +0000
  Re: strncpy and 'n' lawrence.jones@siemens.com - 2012-02-16 13:25 -0500
  Re: strncpy and 'n' blp@cs.stanford.edu (Ben Pfaff) - 2012-02-16 11:18 -0800
  Re: strncpy and 'n' Stephen Sprunk <stephen@sprunk.org> - 2012-02-16 13:29 -0600
    Re: strncpy and 'n' Ike Naar <ike@iceland.freeshell.org> - 2012-02-16 22:45 +0000
  Re: strncpy and 'n' ImpalerCore <jadill33@gmail.com> - 2012-02-16 11:32 -0800
  Re: strncpy and 'n' Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-02-17 00:50 -0800

csiph-web