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


Groups > comp.lang.c > #164681

Re: on how to use the third argument of strncmp()

Path csiph.com!aioe.org!SZor4ZqbvgPVVsWQ21e4cw.user.46.165.242.75.POSTED!not-for-mail
From Meredith Montgomery <mmontgomery@levado.to>
Newsgroups comp.lang.c
Subject Re: on how to use the third argument of strncmp()
Date Fri, 28 Jan 2022 18:17:42 -0300
Organization Aioe.org NNTP Server
Message-ID <86ee4rblvt.fsf@levado.to> (permalink)
References <86czkbd4ey.fsf@levado.to> <st1idr$jc8$1@gioia.aioe.org>
Mime-Version 1.0
Content-Type text/plain
Injection-Info gioia.aioe.org; logging-data="21660"; posting-host="SZor4ZqbvgPVVsWQ21e4cw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Cancel-Lock sha1:i6s0ov0ihWou2mAXhuBdCsyE6dM=
X-Notice Filtered by postfilter v. 0.9.2
Xref csiph.com comp.lang.c:164681

Show key headers only | View raw


Mateusz Viste <mateusz@xyz.invalid> writes:

> 2022-01-28 at 16:52 -0300, Meredith Montgomery wrote:
>> What should choose for the n argument?  If I'm sure s1 and s2 are
>> c-strings, then I have nothing to worry about, but if I have to
>> specify the n argument, then I must choose a number.  To me the
>> sensible thing is
>> 
>>   min(strlen(s1), strlen(s2))
>> 
>> So far so good.  What if I don't want to trust that s1 and s2 are
>> c-strings?  In that case I can't use strlen() at all.  So I guess this
>> procedure assumes c-strings.
>
> This function is intended to work with fixed-width (padded, not
> necessarily terminated) string fields, like strncpy(). I don't think
> this kind of fields is much in use nowadays.
>
> strncmp() also allows to compare prefix of strings (ie. "do s1 and s2
> start with the exact same 7 characters?" situation).
>
> Since you wish to compare NULL-terminated strings in their entirety,
> the usual strcmp() is the right tool for the job.

But what do I do when these strings come from a user?  I can't trust the
user will close them properly.  

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


Thread

on how to use the third argument of strncmp() Meredith Montgomery <mmontgomery@levado.to> - 2022-01-28 16:52 -0300
  Re: on how to use the third argument of strncmp() Mateusz Viste <mateusz@xyz.invalid> - 2022-01-28 21:07 +0100
    Re: on how to use the third argument of strncmp() Meredith Montgomery <mmontgomery@levado.to> - 2022-01-28 18:17 -0300
      Re: on how to use the third argument of strncmp() Mateusz Viste <mateusz@xyz.invalid> - 2022-01-28 22:35 +0100
      Re: on how to use the third argument of strncmp() Vir Campestris <vir.campestris@invalid.invalid> - 2022-02-02 22:01 +0000
    Re: on how to use the third argument of strncmp() Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-01-28 18:37 -0800
      Re: on how to use the third argument of strncmp() scott@slp53.sl.home (Scott Lurndal) - 2022-01-29 15:32 +0000
      Re: on how to use the third argument of strncmp() Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-29 19:02 +0100
  Re: on how to use the third argument of strncmp() scott@slp53.sl.home (Scott Lurndal) - 2022-01-28 21:26 +0000
    Re: on how to use the third argument of strncmp() Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2022-01-28 22:03 +0000
      Re: on how to use the third argument of strncmp() scott@slp53.sl.home (Scott Lurndal) - 2022-01-29 15:31 +0000
  Re: on how to use the third argument of strncmp() Siri Cruise <chine.bleu@yahoo.com> - 2022-01-28 13:43 -0800
    Re: on how to use the third argument of strncmp() Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-29 13:43 +0100
      Re: on how to use the third argument of strncmp() Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-29 16:06 +0100
        Re: on how to use the third argument of strncmp() Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-29 16:09 +0100
  Re: on how to use the third argument of strncmp() Kaz Kylheku <480-992-1380@kylheku.com> - 2022-01-28 21:54 +0000
    Re: on how to use the third argument of strncmp() Meredith Montgomery <mmontgomery@levado.to> - 2022-01-28 21:47 -0300
  Re: on how to use the third argument of strncmp() James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-28 19:40 -0500
    Re: on how to use the third argument of strncmp() Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-29 03:53 -0800
      Re: on how to use the third argument of strncmp() Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-29 19:01 +0100
    Re: on how to use the third argument of strncmp() Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2022-01-29 10:54 -0700
      Re: on how to use the third argument of strncmp() James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-29 14:55 -0500
  Re: on how to use the third argument of strncmp() Manfred <noname@add.invalid> - 2022-01-30 04:03 +0100
    Re: on how to use the third argument of strncmp() Meredith Montgomery <mmontgomery@levado.to> - 2022-01-30 10:02 -0300
      Re: on how to use the third argument of strncmp() Dolores Filandro <dolfiland8@gmail.com> - 2022-02-03 18:48 -0800

csiph-web