itoa [英] itoa

查看:78
本文介绍了itoa的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想要的是一个简单的函数,它接受一个int,并返回一个char *


所以我试过


char * itoa(int i)

{

char buff [35];

返回_itoa(i, buff,10);

}


但是所有这些都是乱七八糟的。

i不明白为什么它'必须传入一个c字符串,

使用一个函数来修改它,然后打印它。当我想要做一些像


printf("%s",itoa(my_int));
的时候,它会让我感到非常恼火

但是noo ......!相反,我必须这样做


char my_string [32];

itoa(my_int,my_string,10);

printf( %s,my_string);


等等。


无论如何......我确定那里有解决方案。有没有人知道呢?


all i want is a simple function that takes an int, and returns a char*

so i tried

char * itoa(int i)
{
char buff[35];
return _itoa(i,buff,10);
}

but all it spits out is jibberish.
i don''t understand why it''s so necessary to have to pass in a c-string,
use a function to modify it, and then print it. it makes it highly
irritating when i want to do something like

printf("%s", itoa(my_int));

but noo...! instead i have to do

char my_string[32];
itoa(my_int,my_string,10);
printf("%s, my_string);

blah.

anyways... i''m sure there''s a solution out there. does anyone know it?

推荐答案

Mark写道:

我想要的一切是一个简单的函数,它接受一个int,并返回一个char *

所以我试过了

char * itoa(int i)
{
char buff [35];
返回_itoa(i,buff,10);
}

但它吐出来的都是乱七八糟的。
我不明白为什么必须传入一个c-string,
使用一个函数来修改它,然后打印它。当我想要做一些像

printf("%s",itoa(my_int));

但是noo时,它会让人非常恼火。 。!而我必须做的事情

char my_string [32];
itoa(my_int,my_string,10);
printf("%s,my_string);

等等。

无论如何......我确定那里有解决方案。有谁知道呢?

all i want is a simple function that takes an int, and returns a char*

so i tried

char * itoa(int i)
{
char buff[35];
return _itoa(i,buff,10);
}

but all it spits out is jibberish.
i don''t understand why it''s so necessary to have to pass in a c-string,
use a function to modify it, and then print it. it makes it highly
irritating when i want to do something like

printf("%s", itoa(my_int));

but noo...! instead i have to do

char my_string[32];
itoa(my_int,my_string,10);
printf("%s, my_string);

blah.

anyways... i''m sure there''s a solution out there. does anyone know it?




C ++常见问题怎么样:
http://www.parashift.com/c++-faq-lit...al-issues.html


问候,

Peter Jansson



How about the C++ faq:
http://www.parashift.com/c++-faq-lit...al-issues.html

Regards,
Peter Jansson


一种难看的圆形解决方案,但它有效;谢谢


const char * itoa(int i)

{

ostringstream o;

o< ;< i;

返回o.str()。c_str();

}


不要以为我应该使用std :字符串,所以我可以返回一个

字符*虽然。

kind of an ugly round-about solution, but it works; thanks

const char * itoa(int i)
{
ostringstream o;
o << i;
return o.str().c_str();
}

dont think i should have to use std:strings so that i can return a
char* though.


一种难看的圆形解决方案,但它作品;谢谢


const char * itoa(int i)

{

ostringstream o;

o< ;< i;

返回o.str()。c_str();

}


不要以为我应该使用std :字符串,以便我可以返回

字符*。

kind of an ugly round-about solution, but it works; thanks

const char * itoa(int i)
{
ostringstream o;
o << i;
return o.str().c_str();
}

dont think i should have to use std:strings so that i can return a
char* though.


这篇关于itoa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆