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


Groups > comp.lang.c > #3041

Re: How to use asprintf consecutively

From Ike Naar <ike@sverige.freeshell.org>
Newsgroups comp.lang.c
Subject Re: How to use asprintf consecutively
Date 2011-04-29 15:20 +0000
Organization A noiseless patient Spider
Message-ID <slrn3vfsirllqn.hng.ike@sverige.freeshell.org> (permalink)
References <48eee806-963d-4e35-baab-0ceb43396071@q32g2000yqn.googlegroups.com>

Show all headers | View raw


On 2011-04-29, pozz <pozzugno@gmail.com> wrote:
> char *
> create_string(int *array, size_t asize)
> {
>   char *str;
>   while(asize--) {
>     if (asprintf(&str, "%s %d", str, *array++) < 0) {
>       return NULL;
>     }
>   }
>   return str;
> }
>
> I have two doubts here:
> - may I use str for output string and as one of asprintf argument?

On at least one common platform, the manual page for asprintf says:

  The functions asprintf and vasprintf are analogues of sprintf and
  vsprintf, except that they allocate a string large enough to hold the
  output including the terminating NUL, and return a pointer to it via
  the first parameter.

and the manual page for sprintf says:

  If copying takes place between objects that overlap as a result of a
  call to sprintf() or snprintf(), the results are undefined.

So, it's probably not a good idea to use asprintf that way.

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


Thread

How to use asprintf consecutively pozz <pozzugno@gmail.com> - 2011-04-29 02:22 -0700
  Re: How to use asprintf consecutively gordonb.xpugx@burditt.org (Gordon Burditt) - 2011-04-29 05:21 -0500
  Re: How to use asprintf consecutively Ike Naar <ike@sverige.freeshell.org> - 2011-04-29 15:20 +0000
  Re: How to use asprintf consecutively Martin Ambuhl <mambuhl@earthlink.net> - 2011-04-29 11:59 -0400
    Re: How to use asprintf consecutively gazelle@shell.xmission.com (Kenny McCormack) - 2011-04-29 22:40 +0000
  Re: How to use asprintf consecutively Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-04-29 17:19 +0100
  Re: How to use asprintf consecutively Seebs <usenet-nospam@seebs.net> - 2011-04-29 22:38 +0000

csiph-web