我能夠找到最常見的字母,但是當嘗試再次列印 **words 中的元素時,我給出了 \000 值。
我試圖將 head 存盤到另一個變數中,但似乎我仍然失去了所有參考。我也嘗試將它作為常量傳遞,但我仍然失去了參考。void findFrequentLetter(const char **words)并且還用 const 修改了呼叫。我怎樣才能使這項作業?
我知道問題出在第一個for(while())回圈中。
void findFrequentLetter(char const **words) {
int array[255] = {0}; // initialize all elements to 0
char str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i, max;
for (int d = 0; d < nb_words; d) {
while (**words != '\0'){
array[**words];
(*words);
}
words;
}
// Find the letter that was used the most
max = array[0];
int index = 0;
for (i = 0; str[i] != 0; i ) {
if (array[str[i]] > max ) {
max = array[str[i]];
index = i;
}
}
}
uj5u.com熱心網友回復:
這是錯誤的:
for (int d = 0; d < stats->mot_sans_doublons; d)
{
while (**words != '\0')
{
array[**words];
(*words); // <===== HERE
}
words;
}
您的代碼被賦予一個指標陣列的基地址。這些指標中的每一個都指向一個字串。上面有問題的行使該陣列中的指標……永久地前進。底層指標陣列使該陣列中的每個指標永久地走到它們各自字串的末尾。
遍歷這些字串的正確方法是通過中間指標。不要words以任何方式修改內容。例如:
for (int d = 0; d < stats->mot_sans_doublons; d)
{
for (const char *s = words[d]; *s; s)
array[(unsigned char)*s];
}
你的代碼的其余部分是否“有效”我留給你,但這就是為什么在呼叫你的頻率計數器之后,你的 words 陣列似乎已經“失去了它的參考”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/447664.html
上一篇:需要想法如何決議以下JSON格式
