convert int to C string

A

Andrej Viktorovich

Hello,

Moving from delphi to C :)

Got garbage on output while converting integer to C string. Have feeling that something wrong with char* on intToStr return. What is the right way of doing that?

char* intToStr(int value)
{
char buffer[33];
char* c;
c= itoa(value,buffer,10);
return buffer;
}

int _tmain(int argc, _TCHAR* argv[])
{

cout<<intToStr(55)<<"\n";

getc(stdin);

return 0;
}

Thank you
 
A

Alain Ketterlin

Andrej Viktorovich said:
Moving from delphi to C :)

Sorry, this group is about C++. Try other, more relevant newsgroups.
Got garbage on output while converting integer to C string. Have
feeling that something wrong with char* on intToStr return. What is
the right way of doing that?

char* intToStr(int value)
{
char buffer[33];
char* c;
c= itoa(value,buffer,10);
return buffer;
}

You can't return a pointer to a local array: the storage for the array
disappears at the end of the call.

-- Alain.
 
A

Andrej Viktorovich

You can't return a pointer to a local array: the storage for the array
disappears at the end of the call.

So only way is to create char[] in main method and pas it to function that makes conversion from integer to string?

Thank you
 
B

BGB

Sorry, this group is about C++. Try other, more relevant newsgroups.

noting that he was using 'cout' in the snipped portion, means it was C++
anyways...

kind of funny that some of the first responses on the C newsgroup were
"this code is C++, not C".

Got garbage on output while converting integer to C string. Have
feeling that something wrong with char* on intToStr return. What is
the right way of doing that?

char* intToStr(int value)
{
char buffer[33];
char* c;
c= itoa(value,buffer,10);
return buffer;
}

You can't return a pointer to a local array: the storage for the array
disappears at the end of the call.

yep, some other means would be needed to return the string.

there are simple ways to deal with this using the static keyword, but
many regard this as "evil", and is not thread-safe, ...

other options include specialized allocators, using heap allocated
memory, ...

or, possibly, something like std::string or similar.
 
K

Kevin McCarty

Hello,

Moving from delphi to C :)

Actually it would appear you're moving to C++, given the use of "cout"
in your code sample.

Got garbage on output while converting integer to C string. Have feeling that something wrong with char* on intToStr return. What is the right way of doing that?

char* intToStr(int value);

// ...
int _tmain(int argc, _TCHAR* argv[])
{

        cout<<intToStr(55)<<"\n";

  getc(stdin);

  return 0;
}


Assuming a C++ 2011 compiler, try:

#include <string>
#include <iostream>

int main()
{
using std::cout;
using std::endl;
cout << std::to_string(55) << endl;
return 0;
}

Otherwise, if you can use Boost, use

#include <boost/lexical_cast.hpp>
// ...
cout << boost::lexical_cast<std::string>(55) << endl;

Otherwise, use a stringstream:

#include <sstream>
// ..
std::eek:stringstream s;
s << 55;
cout << s.str() << endl;


I'm assuming that the streaming of the string result to std::cout was
just for illustrative purposes. If all you need to do is actually to
print an integer to stdout (or other stream), you can just give the
literal 55 directly to the stream:

cout << 55 << endl;


- Kevin B. McCarty
 
N

Nobody

You can't return a pointer to an *automatic* array (for that reason).
So only way is to create char[] in main method and pas it to function that
makes conversion from integer to string?

You can return a pointer to a static array, e.g.:

char* intToStr(int value)
{
static char buffer[33];
char* c = itoa(value,buffer,10);
return buffer;
}

but that isn't re-entrant, and you'll need to have finished using the
returned pointer before the next call to intToStr().

You can return a pointer to dynamically-allocated memory, e.g.:

char* intToStr(int value)
{
char* buffer = malloc(33);
char* c = itoa(value,buffer,10);
return buffer;
}

but then the caller has to explicitly free() the memory.

In C, having the caller pass in a suitably-sized buffer is usually the way
to go, so long as the caller can determine what is a suitable size without
too much effort.

In C++, you'd normally use a std::string rather than a char* (you can
then use the .c_str() method to get a char* for the data), e.g.:

std::string intToStr(int value)
{
std::eek:stringstream os;
os << value;
return out.str();
}
 
B

Bo Persson

Andrej Viktorovich skrev 2012-06-29 14:43:
Hello,

Moving from delphi to C :)

Got garbage on output while converting integer to C string. Have feeling that something wrong with char* on intToStr return. What is the right way of doing that?

char* intToStr(int value)
{
char buffer[33];
char* c;
c= itoa(value,buffer,10);
return buffer;
}

int _tmain(int argc, _TCHAR* argv[])
{

cout<<intToStr(55)<<"\n";

getc(stdin);

return 0;
}

If this is C++, you don't have to do anything at all. Just output the value:

int _tmain(int argc, _TCHAR* argv[])
{

cout << 55 << "\n";

getc(stdin);

return 0;
}


Too easy?


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top