1

I have some problems with an example of atoi() function from K&R C 2nd edition. Only characters from 0 to 9 should be used. But somewhere in the logic of my program I do something wrong.

So in there is this function:

#include <stdio.h>

int atoi(char s[]);

int main()
{
    int i;
    char ch;
    char co[50]; 
    int  ci[50];

    while(ch != EOF )
    {

        for(i=0;i<50-1 && (ch=getchar()) != EOF && ch != '\n';++i)
        {
            co[i] = ch;
            /*ci[i] = atoi(co[i]);*/ /*bugged*/
            ci[i] = atoi(co);
            printf("%d \n",ci[i]);
        }
        if(ch == '\n')
        {
            co[i] = '\n';
        }
        ++i;
        co[i] = '\0';
    }

    return(0);

}

/* as in the book: */
/* atoi: convert s to integer */

int atoi(char s[])
{
    int i, n;
    n = 0;
    for(i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
    {
        n = 10 * n + (s[i] - '0');
    }

    return(n);
}

Here are the errors I'm getting:

|In function 'main':
19|warning: passing argument 1 of 'atoi' makes pointer from integer without a cast [enabled by default]
3|note: expected 'char *' but argument is of type 'char'
||=== Build finished: 0 errors, 1 warnings (0 minutes, 0 seconds) ===|
Codrin H
  • 27
  • 7
  • 1
    That error message is chrystal clear. – Jim Balter Apr 10 '13 at 18:25
  • @JimBalter That error message is chrystal clear. in fact, fixed, i was parssing the value in a wrong way. edited the main post. – Codrin H Apr 10 '13 at 18:49
  • Sorry for the misspelling, it should be "crystal". Please don't edit your code in place, because it means the answers don't make sense. Corrections should be added as an addendum. – Jim Balter Apr 10 '13 at 22:01

3 Answers3

2

The

(s[i] = '0')

should read

(s[i] - '0')

(note the minus instead of the equals sign).

This converts the characters '0'..'9' to the numeric values 0..9.

You are also not calling atoi() correctly. It takes a string, not a char. You should probably call it from outside the loop.

And ch isn't the right type (it should be int).

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

atoi(); function need pointer to string. char* that is the reason warning warning: passing argument 1 of 'atoi' makes pointer from integer without typecase

you declare co like: char co[50]; but calls atoi(co[i]); this is wrong,

notice it says int not char.

an example like:

atoi("1"); is valid but atoi('1'); not valid.

so even co is like "12345678" then atoi(co) correct but atoi(co[i]) not correct.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • took me a while to understand what you were saying :) – Codrin H Apr 10 '13 at 18:50
  • 1
    @CodrinH okh then two links are important for you (1) [How to convert char to integer in C?](http://stackoverflow.com/questions/868496/how-to-convert-char-to-integer-in-c) and (2) [Why are C character literals ints instead of chars?](http://stackoverflow.com/questions/433895/why-are-c-character-literals-ints-instead-of-chars) You would find interesting. – Grijesh Chauhan Apr 10 '13 at 19:07
  • i still try to understand why i definded the func `atoi(char s[])` and not just `atoi(char s)`, wich are the differences in this cases ? – Codrin H Apr 16 '13 at 17:30
  • @CodrinH First, do you understands `char co[10];` is not same as `char co;` fist is an array and it its null `'\0'` terminated it would be a string in C. Where as second is just a char variable. function `atoi()` excepts a string, and you can't pass a char variable to it. in-effect passing char to atoi() will be an error. – Grijesh Chauhan Apr 16 '13 at 17:37
  • @CodrinH Welcome Codrin, Although I am not sure that I could explain you or not ... – Grijesh Chauhan Apr 16 '13 at 18:05
1
printf("%c = ",co[i]);
ci[i] = atoi(co[i]);
printf("%d \n",ci[i]);

You are trying to convert a char to int, but a char is an integer value. All you need is

printf("%c = %d\n", co[i], co[i]);

if what you want is the decimal value of the char. If what you're trying to do is convert an ASCII digit to an integer, then

printf("%c = %d\n", co[i], co[i] - '0');

will do.

Jim Balter
  • 16,163
  • 3
  • 43
  • 66
  • i managed this way:` int atoi(char s[]) { int n; n = (int)(s); return n; }` – Codrin H Apr 10 '13 at 18:37
  • @CodrinH That makes no sense. I think you need to read and study K&R or some other C language tutorial until you have a better understanding of the language. – Jim Balter Apr 10 '13 at 18:39