[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: small c problem



On Thu, Jul 01, 1999 at 03:19:05PM +1200, Michael Beattie wrote:
> 
> The following problem will help me finish v0.1 of my wizard radio control
> program which I will ITP and upload soon..
> 
> <prepared for flames>
> 
> I know its the wrong place to ask, but its really easy, and I feel stupid
> asking in a newsgroup....
> 
> string with value "number:"
> 
> integer with value "444"
> 
> I want the string to then have the value "number:444"
> 
> this is the actual C code:  (slightly snipped)
> 
>    int freq_num;
>    double freq;
>    char *rad_cmd;
> 
>    freq = atof(optarg);   // optarg is "98.2"
> 
>    freq_num = (int) ((freq - 76) / 0.05);   // gives 444
> 
>    rad_cmd = "number:";
> 
>    What now?  i.e.  rad_cmd+freq_num to give rad_cmd = "number:444"
>    strcat()?
> 
> 
> </prepared for flames>

No, well, yes.. well.. for starters, you need a buffer, not a pointer.
rad_cmd has no space to hold "number:444".  Change it to 
char rad_cmd[x] where x is some number that will hold the maximum size of
the string + 1.

Then you can do...

sprintf( rad_cmd, "number:%u", freq_num );

-- 
Please cc all mailing list replies to me, also.
=========================================================================
* http://benham.net/index.html        <gecko@benham.net>           <><  *
* -------------------- * -----------------------------------------------*
* Debian Developer, Debian Project Secretary, Debian Webmaster          *
* <gecko@debian.org> <secretary@debian.org> <lintian-maint@debian.org>  *
* <webmaster@debian.org> <gecko@fortunet.com> <webmaster@spi-inc.org>   *
=========================================================================

Attachment: pgpI1XSfFvSym.pgp
Description: PGP signature


Reply to: