Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing argument 1 of 'atoi' makes pointer from integer without a cast…can any body help me

Tags:

c

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char c;
    int count = 0;
    c=fgetc(file);
    while  (c != '\n' )
    {
        instruction_file[count] = atoi(c);
        c = fgetc(file);
        count++;
    }
}

The error message is

warning: passing argument 1 of 'atoi' makes pointer from integer without a cast
/usr/include/stdlib.h 147, expected const char* but argument of type char
like image 596
Rameshwar Prasad Meghwal Avatar asked Dec 26 '22 04:12

Rameshwar Prasad Meghwal


1 Answers

It looks like you are trying to use atoi to parse single-digit numbers. However, since atoi expects a C string and takes a const char*, you cannot pass it a plain char. You need to pass it a properly terminated C string:

char c[2] = {0};
c[0]=fgetc(file);
instruction_file[count] = atoi(c); // This will compile

However, this is not the most efficient way of interpreting a digit as a numeric value: you can do the same thing faster by subtracting 0 from the digit:

char c;
...
instruction_file[count] = c - '0';
like image 158
Sergey Kalinichenko Avatar answered Feb 13 '23 21:02

Sergey Kalinichenko