Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

She sells cshs by the cshore.


devel / comp.lang.c / Re: on how to use the third argument of strncmp()

SubjectAuthor
* on how to use the third argument of strncmp()Meredith Montgomery
+- Re: on how to use the third argument of strncmp()Stefan Ram
+* Re: on how to use the third argument of strncmp()Mateusz Viste
|+* Re: on how to use the third argument of strncmp()Meredith Montgomery
||+- Re: on how to use the third argument of strncmp()Mateusz Viste
||`- Re: on how to use the third argument of strncmp()Vir Campestris
|`* Re: on how to use the third argument of strncmp()Keith Thompson
| +- Re: on how to use the third argument of strncmp()Scott Lurndal
| `- Re: on how to use the third argument of strncmp()Bonita Montero
+* Re: on how to use the third argument of strncmp()Scott Lurndal
|`* Re: on how to use the third argument of strncmp()Mike Terry
| `- Re: on how to use the third argument of strncmp()Scott Lurndal
+* Re: on how to use the third argument of strncmp()Siri Cruise
|`* Re: on how to use the third argument of strncmp()Bonita Montero
| `* Re: on how to use the third argument of strncmp()Bonita Montero
|  `- Re: on how to use the third argument of strncmp()Bonita Montero
+* Re: on how to use the third argument of strncmp()Kaz Kylheku
|`- Re: on how to use the third argument of strncmp()Meredith Montgomery
+* Re: on how to use the third argument of strncmp()James Kuyper
|+* Re: on how to use the third argument of strncmp()Malcolm McLean
||`- Re: on how to use the third argument of strncmp()Bonita Montero
|`* Re: on how to use the third argument of strncmp()Joe Pfeiffer
| `- Re: on how to use the third argument of strncmp()James Kuyper
`* Re: on how to use the third argument of strncmp()Manfred
 `* Re: on how to use the third argument of strncmp()Meredith Montgomery
  `- Re: on how to use the third argument of strncmp()Dolores Filandro

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

<7c3858a4-4faa-4cc3-ac1f-18dc4a31931cn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20329&group=comp.lang.c#20329

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:199e:: with SMTP id u30mr746076qtc.564.1643942885461;
Thu, 03 Feb 2022 18:48:05 -0800 (PST)
X-Received: by 2002:a05:6214:226c:: with SMTP id gs12mr617786qvb.4.1643942885291;
Thu, 03 Feb 2022 18:48:05 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 3 Feb 2022 18:48:05 -0800 (PST)
In-Reply-To: <86k0eh5qcp.fsf@levado.to>
Injection-Info: google-groups.googlegroups.com; posting-host=108.54.83.172; posting-account=BtBOEwoAAAAFZDKynGutKL1bxHMXo3Ay
NNTP-Posting-Host: 108.54.83.172
References: <86czkbd4ey.fsf@levado.to> <st4v66$1624$1@gioia.aioe.org> <86k0eh5qcp.fsf@levado.to>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7c3858a4-4faa-4cc3-ac1f-18dc4a31931cn@googlegroups.com>
Subject: Re: on how to use the third argument of strncmp()
From: dolfila...@gmail.com (Dolores Filandro)
Injection-Date: Fri, 04 Feb 2022 02:48:05 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 39
 by: Dolores Filandro - Fri, 4 Feb 2022 02:48 UTC

On Sunday, January 30, 2022 at 8:02:26 AM UTC-5, Meredith Montgomery wrote:
> Manfred <non...@add.invalid> writes:
>
> > On 1/28/2022 8:52 PM, Meredith Montgomery wrote:
> >> I don't really get
> >> --8<---------------cut here---------------start------------->8---
> >> int strncmp(const char *s1, const char *s2, size_t n);
> >> The strncmp() function shall compare not more than n bytes (bytes
> >> that
> >> follow a NUL character are not compared) from the array pointed to by
> >> s1 to the array pointed to by s2.
> [...]
> > The more common use for strncmp is to test if two strings have a
> > common prefix.
> >
> > A more peculiar use is in the example given by James Kuyper: you get
> > strings in some constrained buffer, and the terminating '\0' is
> > included only if there is enough space in the buffer; in this scenario
> > you may set the third argument as the size of the buffer so as to
> > ensure that you don't read beyond the buffer limits.
> This makes perfect sense. I'm finally at peace with strncmp() and the
> others of the family. Thank you!

As an example of something like that,
if you wanted to write strstr() in C, you could use strncmp().

char *strstr(const char *s1, const char *s2)
{ const int c = *s2++;

if (c != '\0') {
const size_t n = strlen(s2);

s1 = strchr(s1, c);
while (s1 != NULL && strncmp(s1 + 1, s2, n) != 0) {
s1 = strchr(s1 + 1, c);
}
}
return (char *)s1;
}

Pages:12
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor