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