Andrej Viktorovich <> writes:
>> Assuming that C++ differs little from C in this respect, you're
>> returning the address of a buffer which is local to the invocation of
>> intToStr. It's not a good idea.
>
> I have feeling that in C i must create memory area and pass it like
> parameter to function that converts int to string.
>
> But I would like to have simple function with one param of integer and
> I would like to ask intToString to do all that job of creating memory
> area and I need just char[] on utput. Is it possible to think that way
> in C?
Yes, but you have to think *really hard*.

}
You can't just return a string or array from a C function. It has to be
allocated somewhere, and you pretty much have to manage that allocation
yourself.
There are (at least) three approaches:
1. Declare the local buffer as "static", so its lifetime extends over
the entire execution of the program. This has some drawbacks. There's
only one buffer in memory, so successive calls will clobber the value
from previous calls. And you have to decide how big the buffer needs to
be.
2. Have the caller pass in a pointer to (the first element of) its own
buffer.
3. Have the function allocate a new buffer using malloc(). This makes
the caller responsible for free()ing the buffer when it's done with it.
(Since you're using "cout << ...", you might consider using C++
mechanisms for this, like std::string. But if you have any questions
about C++, you'll need to ask in comp.lang.c++.)
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"