C编译器错误:“指针和整数之间的比较”。

时间:2016-03-17 21:01:04

标签: c compiler-errors scanf

我在过去几天使用fscanf()完成了一些程序,但没有收到此错误。它在while循环条件语句的第21行。该程序从控制台传递第二个参数,该控制器是一个包含非常大的mersenne素数的文件,并将内容读入程序。从那里我将数字的总数加上每个不同的可能数字的数量(即0,1,2等),然后将结果打印到控制台。任何帮助将不胜感激。

#include <stdio.h>

int main(int argc, char* argv[])
{
        FILE *file;
        int digit;
        unsigned long int zeros = 0;
        unsigned long int ones = 0;
        unsigned long int twos = 0;
        unsigned long int threes = 0;
        unsigned long int fours = 0;
        unsigned long int fives = 0;
        unsigned long int sixes = 0;
        unsigned long int sevens = 0;
        unsigned long int eights = 0;
        unsigned long int nines = 0;
        unsigned long int total = 0;

        file = fopen(argv[1], "r");

        while (fscanf(file, "%d", &digit) != NULL)
        {
                if (digit == 0)
                {
                        zeros += 1;
                }

                else if (digit == 1)
                {
                        ones += 1;
                }
                else if (digit == 2)
                {
                        twos += 1;
                }
                else if (digit == 3)
                {
                        threes += 1;
                }
                else if (digit == 4)
                {
                        fours += 1;
                }
                else if (digit == 5)
                {
                        fives += 1;
                }
                else if (digit == 6)
                {
                        sixes += 1;
                }
                else if (digit == 7)
                {
                        sevens += 1;
                }
                else if (digit == 8)
                {
                        eights += 1;
                }
                else if (digit == 9)
                {
                        nines += 1;
                }
                total += 1;
        }

        printf("0s: %u\n1s: %u\n2s: %u\n3s: %u\n4s: %u\n5s: %u\n6s: %u\n7s: %u\n8s: %u\n9s: %u\nTotal: %u\n", zeros, ones, twos, threes, fours, fives, sixes, sevens, eights, nines, total);

        fclose(file);

        return 0;
}

1 个答案:

答案 0 :(得分:2)

fscanf的返回类型为int。因此,编译器抱怨使用:

    while (fscanf(file, "%d", &digit) != NULL)

将其更改为:

    while (fscanf(file, "%d", &digit) == 1)