-
Problem with atoi()
My program compiles with one warning: passing arg 1 of `atoi' makes pointer from integer without a cast.
When I run the program it crashes and the line causing the problems looks like this:
Code:
map[y][x] = atoi(rdBuffer[x]);
map is an 2d array of integers and rdBuffer is a char array which gets a string from a file.
If anyone could help me with the cast I would really appreciate it!
-
rdBuffer[x] is a char, atoi needs a char pointer. you should either send just rdBuffer or send (rdBuffer + x) if you want to start from the x offset of the char array.
-
The cause of the error is that atoi() requires a char * argument, and rdBuffer[x] is a char. Assuming that x is where the string that you wish to convert to an int starts within rdBuffer, something like:
Code:
map[y][x] = atoi(&rdBuffer[x]);
However, given that x is also used as an index in the access to map, I'm guessing what you really are looking for is the value of a digit character at position x within the input buffer, in which case you're really looking for something more like:
Code:
map[y][x] = rdBuffer[x] - '0';
Note that this does no error/range checking, so non-ASCII digit characters will end up giving you bogus values in your map. You should check for these.
-
It now compiles without warnings, thank you both for your fast replies!!