發信人: Wyvern (你猜), 信區: Program
標 題: Re: 問一個function
發信站: 杏花村 (Tue Feb 25 13:18:50 1997) , 站內信件
【 在 wenny (溫妮村姑) 的大作中提到: 】
: 在UNIX 上要把整數轉成字串要用那一個 FUNCTION
: 如字串轉成整數是用 atoi() 那整數轉字串呢??
there is no itoa() in UNIX, but you can write your own..
char itoa (const int i)
{
static char buf [ MAX_INPUT_LENGTH ];
sprintf (buf, "%d", i);
return buf;
}
and since this function return a static memory location, it is your
responsibility to duplicate by yourself. For example, statements as
follow will cause an error.
fprintf (fp, "Affect %s %s\n",
itoa (af.type), itoa (af.level));
But the itoa() can be changed into follow form:
char itoa (const int i)
{
static char buf_queue [MAX_QUEUE_SIZE][MAX_INPUT_LENGTH];
static int queue_pointer;
char *return_str;
return_str = buf_queue [queue_pointer];
sprintf (return_str, "%d", i);
queue_pointer++;
queue_pointer %= MAX_QUEUE_SIZE;
return return_str;
}
Then statements as fprintf above might be allowed if the time of invoking
itoa() in the argument list is less than MAX_QUEUE_SIZE.