所以我試圖找到一種方法來更新一個變量,我試圖讓它成爲全局變量,所以我可以輕鬆地進行更改。唯一的問題是它不工作,我不知道如何解決它。更改全局變量的值,C
我希望SIZE_ARRAY在每次調用remove_unimportant_words函數時都變爲它變成的值。
Decleration:
int SIZE_ARRAY = 480;
char list[SIZE_ARRAY][MAX];
void remove_unimportant_words(char word[MAX], int SIZE_ARRAY, char list[SIZE_ARRAY][MAX] , int j, int i);
INT主要():
while (fscanf(file, "%s", word) != EOF){
remove_unimportant_words(word, SIZE_ARRAY, list, j, i);
}
功能:
void remove_unimportant_words(char word[MAX], int SIZE_ARRAY, char list[SIZE_ARRAY][MAX] , int j, int i)
{
for (i=0; i<SIZE_ARRAY; i++) {
if(strcmp(list[i],word) == 0){
for (j=i; j<SIZE_ARRAY; j++) {
strcpy(list[j], list[j+1]);
}
SIZE_ARRAY--;
i--;
}
}
printf("%d\n", SIZE_ARRAY);
}
我已經基本上ŧ打印SIZE_ARRAY的值,並且在進入該函數時總是從480開始。
請問你能解釋一下這個void remove_unimportant_words(char word [MAX],int SIZE_ARRAY,char list [SIZE_ARRAY] [MAX],int j,int i );'? –
'remove_unimportant_words'函數中的'SIZE_ARRAY'是一個完全獨立的變量;修改它不會修改全局變量。 –
您正在傳遞'SIZE_ARRAY'的副本,而不是全局的SIZE_ARRAY變量本身。將參考/指針傳遞給SIZE_ARRAY,或者直接從函數內部訪問它。 – initramfs