Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #164697
| From | James Kuyper <jameskuyper@alumni.caltech.edu> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: on how to use the third argument of strncmp() |
| Date | 2022-01-28 19:40 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <st22e8$m8m$1@dont-email.me> (permalink) |
| References | <86czkbd4ey.fsf@levado.to> |
On 1/28/22 14:52, 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. > --8<---------------cut here---------------end--------------->8--- > > --8<---------------cut here---------------start------------->8--- > Definition. A c-string is a 0-terminated array of characters. > --8<---------------cut here---------------end--------------->8--- > > 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. > > Is that the end of the story? I feel there could be more to this. Can > you share your experience? Thank you. I've been told by experts that the reasons why I needed to use strncpy() were very peculiar, but given those peculiar reasons, it's use was legitimate. The situation described below came up fairly frequently while I was working for NASA on data coming down from earth-observing satellites: My programs had to deal with file formats that set aside a fixed amount of space to store a string that was not guaranteed to be able to fit in that space - if it didn't, it was acceptable to truncate it to fit. There would be a terminating null character only if the field stored a string shorter than the size of the field. I used strncpy() both to fill in such fields, and to copy from such fields. In both cases, the third argument of strncpy() was the size allocated for that field, which I knew at the time I was writing the program because I could read the file's format specification. strlen() would have been useless for determining that value - since the data was not guaranteed to be null terminated, strlen() might easily have ended up searching outside the memory allocated for the string for a terminating null character. There was no guarantee that it would find one before reaching memory that my process did not have permission to read. Any null character found outside of the memory allocated for the string would have had nothing to do with describing the length of the string.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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