What is wrong with the following code? (itoa function)

Hello, I had a C++ interview question about the following problem.

What is wrong with the following code?
1
2
3
4
5
6
7
8
9
10
11
 

int main()
{
char* a = new char[16];
a = itoa(300, a, 10);
printf("%s \n", a);

system("pause");
return 0;
}


I didn't see anything wrong with it at first glance and I still can't find the problem today.

According to the documentation, everything looks fine too...:
1
2
 
char *  itoa ( int value, char * str, int base );


Can anyone explain what is actually wrong with the code and why it doesn't work?
Thank you.

Besides not deleting the array (which shouldn't really be a problem since when the program ends, the OS cleans things up), the function itoa is non-standard. It also assumes WIndows with the system("pause") crap. It's also a C++ program that's using printf, but that's just bad style.
Last edited on
Well the first thing may be that itoa() is a non-standard function and may not be available with all compilers.

Next why the dynamic memory?

Do you realize that you really don't need that assignment in this snippet?

Why a = itoa(300, a, 10); and not just itoa(300, a, 10);
What did the error say?
Registered users can post here. Sign in or register to post.