标签:style blog http color os io strong ar 2014
1、任意位的整数转化为字符串
#include<stdio.h> #include<iostream> using namespace std; char * NumberToString(int n) { int nn=n,i=0; while(nn!=0) { nn=nn/10; i++; } char *temp=new char[i+1];//可实现任意位的数字的转化 temp[i--]=‘\0‘; while(0!=n) { temp[i]=n%10+‘0‘; n=n/10; i--; } return temp; } void main() { int n=12321; char *str=NumberToString(n); 方法1,这里的str指向堆内存,结束要自己释放 // char *str=new char[7]; //itoa(n,str,10); //方法2,库函数 cout<<str<<endl; delete str; }
2、函数中不能返回在栈上分配的内存
http://blog.csdn.net/a_sungirl/article/details/10552447
标签:style blog http color os io strong ar 2014
原文地址:http://www.cnblogs.com/yexuannan/p/3940932.html