Sorry.. I missed that part.. I have done this type of thing before with much
success, and without the static, but I usually am not tweaking someone
else's. Sorry.

Here is the final that I came up with that works fine (without a static):

char *fnum( long number )
{
    int i, rest;
    int n = 0;
    char buf[MSL];
    char buf_new[MSL];

    buf[0] = '\0';
    buf_new[0] = '\0';
    sprintf( buf, "%ld", number );
    rest = strlen(buf) % 3;

    for ( i = 0; i < strlen(buf); i++ )
    {
        if ( i != 0 && ( (i - rest) % 3 ) == 0 )
            buf_new[n++] = ',';

        buf_new[n++] = buf[i];
    }
    buf_new[n] = '\0';

    return str_dup(buf_new);
}

Hope this clears up my thought process for you.

-V


----- Original Message ----- 
From: "Michael Barton" <[EMAIL PROTECTED]>
To: "Valnir" <[EMAIL PROTECTED]>; "ROM List" <[email protected]>
Sent: Thursday, February 12, 2004 1:56 PM
Subject: Re: Re: Formatting numbers


> This won't work.
> You're not allocating memory for buf_new anywhere.
>
> ----- Original Message ----- 
> From: "Valnir" <[EMAIL PROTECTED]>
> To: "ROM List" <[email protected]>
> Sent: Thursday, February 12, 2004 11:33 AM
> Subject: Re: Re: Formatting numbers
>
>
> > Well, I didn't mean dump the pointer. Sorry for the confusion.
> >
> > Here is my suggestion.
> >
> > char *fnum(long number)
> > {
> >      int index,index_new,rest;
> >      char buf[16];
> >      char *buf_new;
> >
> >      sprintf(buf, "%ld", number);
> >      rest = strlen(buf)%3;
> >
> >      for (index = index_new = 0;index < strlen(buf); index++,
index_new++)
> >      {
> >           if (index != 0 && (index - rest) %3 == 0 )
> >           {
> >                buf_new[index_new]=',';
> >                index_new++;
> >                buf_new[index_new] = buf[index];
> >           }
> >           else
> >                buf_new[index_new] = buf[index];
> >      }
> >      buf_new[index_new] = '\0';
> >      return buf_new;
> > }
> >
> > There, I think that will work... basically the same function, just
change
> > the "static char buf_new[16];" to "char *buf_new;" .. I think that will
fix
> > it all together.
> >
> > -V
> >
> > ----- Original Message ----- 
> > From: "mark" <[EMAIL PROTECTED]>
> > To: "Valnir" <[EMAIL PROTECTED]>
> > Cc: <[EMAIL PROTECTED]>
> > Sent: Thursday, February 12, 2004 12:01 PM
> > Subject: Re: Re: Formatting numbers
> >
> >
> > >
> > >    >  Honestly, while I am looking at this, I don't see a reason why
you
> > need to
> > >    >  have it as a static buffer.. Since a static retains the
information
> > in the
> > >    >  memory, and you aren't resetting it at the beginning, then you
have
> > really
> > >    >  one of two choices. Either don't use a static, or make sure you
> > reset the
> > >    >  static buf_new to '\0' at the beginning of the function.
> > >
> > > He needs it to either be static or to return a malloc'ed (in some
manner)
> > memory pointer.  The reason: It's not well defined what happens when you
use
> > variables from another function that don't always exist.  It has to do
with
> > how memory is used across functions - suffice it to say that if it were
not
> > static - it would not always exist when you went to use it.  However,
that
> > does mean that changing that buffer will change every instance of your
> > stuff.  Could be fun. ;-)
> > >
> > > I don't think this is going to make it to the rom list - but I've
BCC'ed
> > it.  Rom list has an issue with UTF-8 emails.
> > >
> > > Anyway, thought this might be enlightening.
> > >
> > > Scout
> > >
> >
> >
> > -- 
> > ROM mailing list
> > [email protected]
> > http://www.rom.org/cgi-bin/mailman/listinfo/rom
>


Reply via email to