将字符串中的字符与 C 中的另一个字符进行比较

Comparing char in a string to another char in C

我拥有的是从 txt 文件中获取的字符串。

fgets(num_string, lenght, file);
num_string = "011110110101"

比起我 select 第一个数字并显示它:

printf("Number: %c", num_string[0]);

之后我的程序通过这个循环获取字符串中的所有数字,然后它应该检查每个数字是 0 还是 1:

for(j=0; j<=11; j++){
                printf("numbers: %c\n", num_string[j]);

                if(strcmp(num_string[j], zero)==0){
                    num_of_zeros++;
                    printf("\nNum of zeros: %d", num_of_zeros);
                }
                else{
                    num_of_ones++;
                    printf("\nNum of ones: %d", num_of_ones);
                }
}

但是 if 语句不起作用。这是它在终端中写入的问题:

AOC_2021_day_3.c: In function 'main':
AOC_2021_day_3.c:27:27: warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast [-Wint-conversion]
                 if(strcmp(num_string[j], zero)==0){
                           ^~~~~~~~~~
In file included from AOC_2021_day_3.c:3:0:
c:\mingw\include\string.h:77:38: note: expected 'const char *' but argument is of type 'char'
 _CRTIMP __cdecl __MINGW_NOTHROW  int strcmp (const char *, const char *) __MINGW_ATTRIB_PURE;
                                      ^~~~~~

如有任何帮助,我将不胜感激:D

strcmp 用于与 cstrings 进行比较。在这种情况下,您根本不需要 strcmp。您想要检查 num_string 中的特定字符是否为 0if(strcmp(num_string[j], zero)==0) 语句可以替换为 if(num_string[j] == '0').