Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: How to copy a string safely and efficiently in C? Date: Mon, 12 Sep 2016 15:30:31 -0700 Organization: None to speak of Lines: 31 Message-ID: References: <0b30b0cc-f7c5-4928-8acb-56bf2364797b@googlegroups.com> <196b9bd0-5b80-4194-9704-014dd19ff6c2@googlegroups.com> <39a5dfd7-a7d3-482f-ada2-c3ec7c3224b5@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="18746"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18TQ7zVpqDuEqm6NneBX2ka" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:JrSxLwg1nV7C3zFT141dONkLfyw= sha1:hWTqEbD18cNnoDEKudWmhGleB9E= Xref: csiph.com comp.lang.c:93403 asetofsymbols@gmail.com writes: > ______ > for copying a sting into a given size buffer, > char dst[size]; > strncpy(dst, src, size); > snprintf(dst, size,"%s", src); > which is better? > -----+++ > for(i=0; i if(i>=size) return -1; > > If the src and destination point > to the same array memmove > but i not remember args... Please learn to quote properly. (Your newsreader should handle this for you.) This part of your followup: > for copying a sting into a given size buffer, > char dst[size]; > strncpy(dst, src, size); > snprintf(dst, size,"%s", src); > which is better? was written by will hunt on September 1. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"