Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #123564
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Updating pointer with result of sprintf |
| Date | 2017-11-28 16:22 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <87fu8ytlt0.fsf@bsb.me.uk> (permalink) |
| References | <ovjd76$sbt$1@dont-email.me> <73dd9def-b2ac-49b6-9aa3-78c8350c86f1@googlegroups.com> <m2r2sibgnj.fsf@despina.home> |
"Pascal J. Bourguignon" <pjb@informatimago.com> writes: > "Rick C. Hodgin" <rick.c.hodgin@gmail.com> writes: > >> On Tuesday, November 28, 2017 at 5:19:35 AM UTC-5, Noob wrote: >>> Hello, >>> >>> Consider the following code: >>> >>> unsigned char src[32]; /* initialized somewhere */ >>> char buf[65]; >> >> I've never seen code which modifies buf. I would change it to >> something like this: >> >> for (i = 0; i < sizeof src && i < sizeof buf / 3; ++i) >> sprintf(buf + (i * 3), "%02x", src[i]); (I think you mean 2 here.) > One advantage of using indexing, is that it allows you to easily fill > the buffer from right-to-left, when you want to dump readably values > that are stored in little-endian order. But using snprintf! > > snprintf(buf + sizeof(buf) - 1 - 2*i, 2, "%02x", src[i]); The expression is wrong (you need buf + size - 1 - 2*(i+1)) but snprintf adds a null so the end result will not be as expected. You can do a little dance, saving the byte that the null will overwrite and putting it back, but it's a bit messy. You get no advantage from using snprintf over plain sprintf here. -- Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar
Updating pointer with result of sprintf Noob <root@127.0.0.1> - 2017-11-28 11:19 +0100
Re: Updating pointer with result of sprintf bartc <bc@freeuk.com> - 2017-11-28 11:02 +0000
Re: Updating pointer with result of sprintf jacobnavia <jacob@jacob.remcomp.fr> - 2017-11-28 12:09 +0100
Re: Updating pointer with result of sprintf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-28 11:23 +0000
Re: Updating pointer with result of sprintf Keith Thompson <kst-u@mib.org> - 2017-11-28 08:58 -0800
Re: Updating pointer with result of sprintf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-28 11:10 +0000
Re: Updating pointer with result of sprintf Spiros Bousbouras <spibou@gmail.com> - 2017-11-28 16:51 +0000
Re: Updating pointer with result of sprintf Keith Thompson <kst-u@mib.org> - 2017-11-28 09:24 -0800
Re: Updating pointer with result of sprintf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-29 00:33 +0000
Re: Updating pointer with result of sprintf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-29 00:36 +0000
Re: Updating pointer with result of sprintf Spiros Bousbouras <spibou@gmail.com> - 2017-11-29 19:04 +0000
Re: Updating pointer with result of sprintf "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-11-28 12:47 +0100
Re: Updating pointer with result of sprintf "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-11-28 12:49 +0100
Re: Updating pointer with result of sprintf scott@slp53.sl.home (Scott Lurndal) - 2017-11-28 14:19 +0000
Re: Updating pointer with result of sprintf gazelle@shell.xmission.com (Kenny McCormack) - 2017-11-28 14:27 +0000
Re: Updating pointer with result of sprintf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-28 14:35 +0000
Re: Updating pointer with result of sprintf "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-11-28 06:31 -0800
Re: Updating pointer with result of sprintf "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-11-28 15:51 +0100
Re: Updating pointer with result of sprintf "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-11-28 07:23 -0800
Re: Updating pointer with result of sprintf "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-11-28 08:16 -0800
Re: Updating pointer with result of sprintf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-28 16:32 +0000
Re: Updating pointer with result of sprintf "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-11-28 08:57 -0800
Re: Updating pointer with result of sprintf "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-11-29 00:47 +0100
Re: Updating pointer with result of sprintf "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-11-29 00:45 +0100
Re: Updating pointer with result of sprintf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-28 16:22 +0000
Re: Updating pointer with result of sprintf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-28 16:25 +0000
Re: Updating pointer with result of sprintf "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-11-29 00:48 +0100
Re: Updating pointer with result of sprintf Noob <root@127.0.0.1> - 2017-11-29 10:07 +0100
Re: Updating pointer with result of sprintf Barry Schwarz <schwarzb@dqel.com> - 2017-11-29 09:18 -0800
csiph-web