getc and atoi

This is a discussion on getc and atoi within the C Programming forums, part of the General Programming Boards category; Code: int i; char a; FILE *input = fopen( "a.c", "r" ); for(i = 0; i < 1000; i++){ if ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    10

    getc and atoi

    Code:
     int i;
         char a;
         FILE *input = fopen( "a.c", "r" );
        
    	for(i = 0; i < 1000; i++){
    	if (( a = getc(input)) == '2')
    		i = (int)atoi(a);
    		printf("%d",i);
    }
    RESULT: Segmentation fault

    I tried with a as an integer
    ???

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    I don't see how that could compile. atoi() works on a string, not a char.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    10
    Quote Originally Posted by bithub View Post
    I don't see how that could compile. atoi() works on a string, not a char.
    So how do I convert char a to a string???

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you turn up the warning level on your compiler, you will probably get a warning saying "converting int to char * without a cast" or some such. It's because you are passing a single char into atoi() which expects a string.

    If you have a single char, you can convert it to an integer by subtracting the zero character (assuming all digits are in the correct sequence 0..9 without gaps, but this holds true for the two most common character encodings ASCII [and ASCII extensions, including UNICODE as long as we use wester-arabic-based diigts] as well as EBCDIC, so we probably cover 99.99% of all system with this).

    Code:
    i = ch - '0';
    With your code, the atoi() function tries to read memory at adress 50, which is invalid in all modern operating systems.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by chocolatecake View Post
    So how do I convert char a to a string???
    I think my original suggestion is better, but if you must:
    Code:
    char str[2];
    ...
    str[0] = ch;
    str[1] = '\0';
    atoi(str);
    But since the conversion from character to integer is so simple, this is like walking around the entire block to get to your next door neighbours house.

    --
    Mats
    Last edited by matsp; 03-30-2009 at 02:17 PM. Reason: Add starting code tag.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    10
    Thank you very much.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21