i have this:
char number[10]
gets(number);
atoi(number[1]);
and it gives me this error:
error C2664: ''atoi'' : cannot convert parameter 1 from ''char'' to ''const char *''
if I just use atoi(number), it works fine.
How come I can''t convert the letters in each index of number[] into a digit, I have tried every possible way of declaring number and I can''t get it to work, is there a better function to convert a letter to an integer?
thanks, SaiSoft
quote: Original post by saisoft
i have this:
char number[10]
gets(number);
atoi(number[1]);
and it gives me this error:
error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
And that is exactly what you are trying to do. "atoi" is array ( ie char*) to integer. You are trying to use a single character ( ie char) and atoi doesn't take a char.
quote:
... is there a better function to convert a letter to an integer?
Two ways:
1) If you only want to convert a single letter to an int then cast to an int. This will give you the ASCII value. You can then subtract off the offset to give the actual number.
2) Convert the char into a char* and use atoi.
// #1int i = (int)number[1];i-=48; // I think that's the right offset. Look at your ASCII chart//#2char buff[2];sprintf(buff,"%c", number[1]);int i = atoi(buff);
At the end of either one i should contain the number.
-------
Andrew
Edited by - acraig on December 14, 2000 11:40:12 AM
char number[10];
allocates an array of 10 chars. So when you use just ''number'', it is a pointer to the beginning of the list (char*).
On the other hand, when you say number[1], you are specifying a particular char data structure (char).
So basically, you need to call:
atoi( &number[1] );
(the & in front specifies "the address of" number[1])
The function doesn''t want a char data structure, it just wants a pointer to one. Two different things. Read up on passing arguments "by reference" vs. "by value" if I wasn''t clear.
Good luck!
allocates an array of 10 chars. So when you use just ''number'', it is a pointer to the beginning of the list (char*).
On the other hand, when you say number[1], you are specifying a particular char data structure (char).
So basically, you need to call:
atoi( &number[1] );
(the & in front specifies "the address of" number[1])
The function doesn''t want a char data structure, it just wants a pointer to one. Two different things. Read up on passing arguments "by reference" vs. "by value" if I wasn''t clear.
Good luck!
Is that right? It was my understanding that atoi() expects a null terminated string and will convert the entire character array to an integer. Ex:
Let the elements of number[] be [''1'',''2'',''3'',''4'',''5''(,0)]
atoi(&number[1]) would return the integer 2345, not 2.
Like Andrew said, if you only want the 2, just do:
digit1=number[1]-''0''; //in ASCII anyway... And make sure you use single quotes around the 0!
Let the elements of number[] be [''1'',''2'',''3'',''4'',''5''(,0)]
atoi(&number[1]) would return the integer 2345, not 2.
Like Andrew said, if you only want the 2, just do:
digit1=number[1]-''0''; //in ASCII anyway... And make sure you use single quotes around the 0!
Nor-easter is correct.
According to the man page atoi uses the NULL as the endpoint for conversion so using atoi(&number[1]) won't perform as expected.
It's the same as atoi( number+1 ) which will start at the 2 and go to the \0.
A little test will show this.
-------
Andrew
Edited by - acraig on December 14, 2000 3:23:19 PM
According to the man page atoi uses the NULL as the endpoint for conversion so using atoi(&number[1]) won't perform as expected.
It's the same as atoi( number+1 ) which will start at the 2 and go to the \0.
A little test will show this.
-------
Andrew
Edited by - acraig on December 14, 2000 3:23:19 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement