1

这是我的一段代码,我想检查下一个是否没有元素,退出程序。但我收到将指针与整数进行比较的警告。我该如何解决它,干杯。

错误是这部分 if(nums[i] == NULL)

/* scanf the file, and store the value in the nums */
for(i = 0; i < numOfInt; i++){

   fscanf(f, "%d", &nums[i]);

   /* check if the value is too big */ 
   if(nums[i] == NULL){  
 printf("arg1 is not equal to nums of element in the file\n");
 exit(1);
   } 
 }
4

3 回答 3

13

查看 的文档fscanf,它不是那样工作的。而是使用它的返回值。

ret = fscanf(f, "%d", &nums[i]);

if (ret != 1)
    /* error */
于 2012-11-05T13:39:55.063 回答
3

让我们假设这nums是一个整数数组。

然后,您将整数与 进行比较NULLNULL用于指针。

就像比较苹果和橘子一样。

熟悉手册页并注意返回值是值得的。

有问题的函数返回解释的项目数。

于 2012-11-05T13:43:56.973 回答
1

我假设 nums 是一个 int[]。NULL 通常是一个 void*(指针),因此将 nums[i] 和 int 与指针进行比较是没有意义的。

于 2012-11-05T13:42:05.700 回答