关于itoa()原函数

来源:百度知道 编辑:UC知道 时间:2024/05/24 06:28:19
#include <stdio.h>
/*Converts an integer to a string. */
char *my_itoa(int value,char *str,int radix)
{
int i,j,sign;
char *ps=NULL;

if((radix>36) || (radix<2)) return 0;

if(value<0)
{
sign=-1;
value=-value;
}

i=0;

do
{
if((value % radix) > 9)
ps[i] = value % radix + '0' + 39;
else
ps[i] = value % radix + '0' ;
i++;
}while((value /= radix) > 0);

if(sign<0)
ps[i]='-';
else i--;

for(j=i;j>=0;j--)
str[j-i]=ps[j];
return str;
}

void main()
{
char *str=NULL;
//my_atoi
char s[]="123456";
printf("integer=%d\n",my_atoi(s));

//my_itoa
printf("str=%s\n",my_itoa(123456,str,10));

}

这个是我自己编写的原函数,但是就是不对,程序运行不出来,但是编译没有错误,各位大侠帮我找找 哪

#include <stdio.h>
/*Converts an integer to a string. */
char *my_itoa(int value,char *str,int radix)
{
int i,j,sign;
char ps[256];
memset(ps,0,256);

if((radix>36) || (radix<2)) return 0;

sign = 0;
if(value<0)
{
sign=-1;
value=-value;
}

i=0;

do
{
if((value % radix) > 9)
ps[i] = value % radix + '0' + 39;
else
ps[i] = value % radix + '0' ;
i++;
}while((value /= radix) > 0);

if(sign<0)
ps[i]='-';
else i--;

for(j=i;j>=0;j--)
str[i-j]=ps[j];
return str;
}

void main()
{
char str[256];
//my_atoi
char s[]="123456";
printf("integer=%d\n",atoi(s));

//my_itoa
memset(str,0,256);
printf("str=%s\n