我擁有的是從 txt 文件中獲取的字符串。
fgets(num_string, lenght, file);
num_string = "011110110101"
所以比我選擇第一個數字並顯示它:
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
是0
或不是。該if(strcmp(num_string[j], zero)==0)
語句可以替換為if(num_string[j] == '0')
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句