Here is a better start. Don't top post. And read the original post
before replying. The OP asked for an implementation that is
equivalent to atoi(), a function that returns the numerical equivalent
of a source text string. What you wrote is just the opposite.
#include <stdlib>
#define ASC_ZERO 48
No, no, NO!!! The macro above is not only unnecessary, it is EVIL.
It will make this function break horribly on implementations that use
an execution character set that is not ASCII. And you don't use the
evil macro anyway.
The character representing 0 in C++ is '0', always, no matter what the
character set.
int Int2Str(int iNum, char* str)
{
char* Refstr = "0123456789";
Completely unnecessary array. And if it was actually necessary,
better defined as:
static const char Refstr [] = "0123456789";
....but it is not needed at all.
int iRem;
int iCnt = 0;
int iflag = 0;
if(iNum < 0)
{
iflag = 1;
iNum *= -1;
This can cause undefined behavior, because INT_MIN may be equal to
(-INT_MAX - 1), so -INT_MIN may overflow.
What happens if the caller passed a character array of 16 elements?
do
{
iCnt ++;
iRem = iNum % 10;
iNum = iNum / 10;
str[63 - iCnt] = Refstr[iRem];
Now you can replace the statement above with:
str[63 = iCnt] = iRem + '0';
....since C++ guarantees that the characters representing the decimal
digits are in contiguous, ascending order.
}while(iNum);
iCnt = 63 - iCnt - iflag;
if(iflag) str[iCnt] = '-';
return(iCnt);
'return' is a statement, not a function call. While parentheses
around the expression are harmless, they have not been required in C
since 1989 and never required in C++.
Oops, you are not programming in C or C++, you just think you are.
In C and C++, main() must be defined with a return type of int.