Having trouble with atoi
I have a function that is returning a string that looks like this:
Then back in my main I have this:
char* getstring(char choice)
does some stuff...
The problem is that atoi keeps returning zero. After tempstring is returned from the function, it is a string which only contains digits, no letters or anything else, except for the null terminator at the end of the string.
tempstring = getstring(mychar);
myinteger = atoi(tempstring);
I have run the program with a breakpoint at the atoi line and I can see that "tempstring" contains an integer. I can read in the Locals box:
"tempstring 0x0013fae8 "80" "
after I step through the atoi line, tempstring is empty and the atoi has put a 0 into myinteger.
I don't understand why this is happening. I have also tried using strtol in place of atoi, but with no luck.
Lemme guess, mystring is a local char array inside the getstring() function.
You can't do this!
The array goes out of scope (along with your data) as soon as you return.
Yeah that was the problem. Thank you!