简体   繁体   English

警告:指针与 integer 比较

[英]Warning: comparison between pointer and integer

I started learning C language and my task is to write a script that will check the text in the txt file for the presence of the word ananas, but you can't use strlen(), strcpy(), strcmp() .我开始学习 C 语言,我的任务是编写一个脚本来检查 txt 文件中的文本是否存在单词 ananas,但您不能使用strlen(), strcpy(), strcmp() I wrote code that doesn't work, but the output is simple 0. I understand why it outputs something, but I don't understand how to do it so it worked fine.我写的代码不起作用,但 output 是简单的 0。我明白它为什么会输出一些东西,但我不知道该怎么做,所以它工作得很好。 And when I compile the code, I get this error当我编译代码时,出现此错误

warning: comparison between pointer and integer
if(c[i] == s || ss){

This is my code这是我的代码

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


int main() {
  FILE *f;
  char c[1000];
  char a[] = {'a'};
  char aa[] = {'A'};

  char n[] = {'n'};
  char nn[] = {'N'};

  char s[] = {'s'};
  char ss[] = {'S'};
  int g = 0;
  int col = 0;

  
  f = fopen("bananas1.txt", "r");
  fgets(c, 1000, f);
  for(int i = 0; i < 1000; i++){
    for(int q = 0; q < 1;){
      if(c[i] == a || aa){
        q++;
      }
      if(c[i] == n || nn){
        q++;
      }
      if(c[i] == s || ss){
        q++;
      }
      if(q == 1){
        g++;
        q = 0;
      }
      if(q == 0){
        g = 0;
      }

    }
    if(g == 6){
      col++;
    }

  }
  printf("%d", col);
  fclose(f);

  return 0;
}

Here is the text that I need to check in the bananas1.txt file这是我需要在bananas1.txt文件中检查的文本

Bananas are edible fruits, botanically berries.香蕉是可食用的水果,植物学上的浆果。 In some countries, bAnAnAs used for cooking are called plantains, distinguishing them from dessert bananaS.在一些国家,用于烹饪的香蕉被称为车前草,以区别于甜点香蕉。 The fruits grow in clusters hanging from the top of the plant.果实成簇生长,悬挂在植物的顶部。 Almost all modern edible seedless BANANAS come from two wild species of Musa acuminata and Musa balbisiana.几乎所有现代食用无核香蕉都来自芭蕉科和芭蕉科两种野生品种。 The scientific names of most cultivated bananas are Musa acuminata, Musa balbisiana, and Musa X paradisiaca, depending on their genomic constitution.大多数栽培香蕉的学名是 Musa acuminata、Musa balbisiana 和 Musa X paradisiaca,具体取决于它们的基因组构成。

As you can see, there is the word bananas, and it contains the word bananas;如您所见,有香蕉这个词,它包含香蕉这个词; I need to count the number of ananas words in this text.我需要计算这篇文章中 ananas 单词的数量。

I assume that this我假设这

 if(c[i] == a || aa){

is meaning to say意思是说

"if c[i] is either 'a' or 'aa' then..." “如果 c[i] 是 'a' 或 'aa' 那么......”

Sadly, C doesn't work like that.遗憾的是,C 并不是那样工作的。 The attempt to compare a char (c[i) with an array of chars (a, even though it's only one character) is why you are getting the error.尝试将一个字符 (c[i) 与一组字符(a,即使它只是一个字符)进行比较是您收到错误的原因。

You need你需要

  if(c[i] == 'a')....

or要么

 char a = 'a';
 if(c[i] == a)...

there is no way to look to see if one char is equal to 2 chars (.).无法查看一个字符是否等于 2 个字符 (.)。 If you need to look for 'aa' then you have to look for an a then look at the next char too如果您需要查找 'aa' 那么您必须查找 a 然后再查看下一个字符

Also you cannot do shortcut 'ors'你也不能做快捷方式'ors'

  if(c[i] == 'a' || 'b')

does not say "if c[i] == a or c[i] == b".不会说“如果 c[i] == a 或 c[i] == b”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM