'atoi' : cannot convert parameter 1 from 'char' to 'const char *'

J

John Smith

What's wrong with the use of atoi in the following code? Why do I
get the error message:

'atoi' : cannot convert parameter 1 from 'char' to 'const char *'


char cBuffer[9];
void PushUnique(int);

for(y = 0; y < 9; y++)
{
isF.getline(cBuffer,9);
for(x = 0; x < 9; x++)
bd.Cell[x][y].PushUnique(atoi(cBuffer[x]));
}
 
C

Chinchilla

the prototype for atoi is
int atoi(const char *str);
meaning it takes a pointer to an entire character string like "12345".

if you wanna get the value of each digit and pass it to PushUnique just
subtract 48 from each character. Hackish, but should work.

bd.Cell[x][y].PushUnique((int)(cBuffer[x] - 48));

Chinchilla
 
V

Victor Bazarov

John said:
What's wrong with the use of atoi in the following code? Why do I
get the error message:

'atoi' : cannot convert parameter 1 from 'char' to 'const char *'


char cBuffer[9];
void PushUnique(int);

for(y = 0; y < 9; y++)
{
isF.getline(cBuffer,9);

Are you sure the buffer of 9 chars is enough to read a whole line?
for(x = 0; x < 9; x++)
bd.Cell[x][y].PushUnique(atoi(cBuffer[x]));

In this statement, the expression inside 'PushUnique's parentheses is

atoi(cBuffer[x])

'cBuffer[x]' is a _char_. 'atoi' expects a pointer to char (actually
a pointer to a first element of an array of char, ending with a null
character, IOW a C string. What are you trying to accomplish here?
Depending on your answer, we can suggest a solution or two. The very
first one that comes to mind is for you to drop the loop here and just
do

bd.Cell[x][y].PushUnique(atoi(cBuffer));

it will compile, but it may not be what you need...

V
 
I

Ivan Vecerina

: What's wrong with the use of atoi in the following code? Why do I
: get the error message:
:
: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
:
:
: char cBuffer[9];
: void PushUnique(int);
:
: for(y = 0; y < 9; y++)
: {
: isF.getline(cBuffer,9);

What do you intend cBuffer to contain here?
I would think it is to contain the ascii representation
of a single number -- such as: "16384" .
When that is the case, a single call to atoi will extract
that number:
int i = atoi(cBuffer); // atoi takes a string, and
// returns the value (e.g. i=16384)
Note that strtoi supports better error reporting,
and shall usually be preferred to atoi.

: for(x = 0; x < 9; x++)
: bd.Cell[x][y].PushUnique(atoi(cBuffer[x]));
: }

Are you trying to convert each digit into an integer?
In this case, PushUnique( cBuffer[x] - '0' ); will do.
But then you need to do the following:
- make sure the read line is at least 9 characters
long, or handle shorter strings appropriately.
- BTW, if you need 9 characters then cBuffer must
be at least 10 chars long (null-termination of C strings).
- error handling: first check that each digit-character
c is actually a decimal digit: ( (c>='0') && (c<='9') )

Ivan
 
O

Old Wolf

Chinchilla said:
the prototype for atoi is
int atoi(const char *str);
meaning it takes a pointer to an entire character string like "12345".

if you wanna get the value of each digit and pass it to PushUnique just
subtract 48 from each character. Hackish, but should work.

bd.Cell[x][y].PushUnique((int)(cBuffer[x] - 48));

Why would you write 48 instead of '0' ?? (Really -- I'm interested,
it seems to be a fairly common practice). It is less portable
and less readable and appears to have absolutely nothing in its
favour, as far as I can see.
 
M

Mike Wahler

Chinchilla:

Please don't omit context (I've restored it below)
Old Wolf said:
Chinchilla said:
bd.Cell[x][y].PushUnique((int)(cBuffer[x] - 48));

Why would you write 48 instead of '0' ?? (Really -- I'm interested,
it seems to be a fairly common practice). It is less portable
and less readable and appears to have absolutely nothing in its
favour, as far as I can see.
force of habit.

All the world is not ASCII.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top