I am trying to convert an integer into a string of numerals stored in char *s
.
char* ItoA(int n, char *s){
int mod = n;
for(int x = 1; x <= sizeof(n) + 2; x++){
int digit = mod % 10;
s[x-1] = digit;
mod = mod / 10;
}
return s;
}
void main(){
int n = 12345;
char s3;
// should print 12345
cout << "\n" << ItoA(n, &s3);
cin.get();
}
I know that something is wrong with my notation because I keep getting an array of garbage as the output in main. Why am I getting garbage instead of "12345"?