As described by @kbw above, the itoa function provided by the standard library has the following three arguments:
argv[0] is the integer to be converted
argv[1] is a pointer to a char array
argv[2] is an integer to tell the itoa function the number base to use
So the problem with the pointer to the memory is overcome by using a memory location the caller has allocated:
1 2 3
|
int number(123); // the number to convert
char buffer[10]; // the char array into which we are to ask itoa to put result
char* result = itoa(number, buffer, 10); // calling itoa using base10
|
If you debug through the above code, you will see that the address to the char array we have declared using the variable named 'buffer' being passed through to itoa function is the same address that is returned by the function and allocated to the char* pointer we have named 'result'. So the memory the result of the function exists outside of the function itself and is therefor not lost like a local variable within itoa would be.
I hope that explains this function.
So, you either provide a particular function with a pointer to the memory to be used as above, OR you can use a member variable inside a struct or class in which the function exists.
HTH
Andrew