我擁有的是從 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
uj5u.com熱心網友回復:
strcmp用于與 cstrings 進行比較。在這種情況下,您根本不需要strcmp。要檢查特定的字符內是否num_string是0或不是。該if(strcmp(num_string[j], zero)==0)陳述句可以替換為if(num_string[j] == '0')。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/373389.html
上一篇:將指標分配給指向char指標的指標,以便在呼叫函式中完成的分配和引數在呼叫函式C中被釋放
下一篇:驗證ID號C#
