簡體   English   中英

數組值在C中突然改變

[英]Array values suddenly changing in C

我正在制作一個具有全局數組的程序。 在將值分配給數組並打印它們的函數中,一切都很好。 但是,當我嘗試在另一個函數中使用這些數組時,值突然不同。

    int *numeros;
    char *operadores;
    int num, num_operadores;

void crearArray() {

    int i;

    printf("How many numbers?\n");
    scanf("%d", &num);

    numeros = (int*)calloc(num, sizeof(int));
    operadores = (char*)calloc(num - 1, sizeof(char));
    num_operadores = num - 1;

    num += (num - 1);

    for (i = 0; i < num; i++) {

        if(i % 2 == 0 || i == 0) {
            printf("\t\nEnter a number: ");
            scanf("%d", &numeros[i]);
        }
        else {
            fflush(stdin);
            printf("\t\nEnter an operator: ");
            scanf("%c", &operadores[i]);
        }

    }

    printf("Array: ");

    for (i = 0; i < num; i++) {

        if(i % 2 == 0 || i == 0)
            printf("%d ", numeros[i]);

        else
            printf("%c ", operadores[i]);

    }

}


void crearArbol() {

    int i;

    printf("\n\nArrays:\n\t Numbers: ");

    for(i = 0; i < num_operadores + 1; i++)
        printf("\n\t\t Numeros[%d]: %d ", i, numeros[i]);

    printf("\n\t Operators: ");

    for(i = 0; i < num_operadores; i++)
        printf("\n\t\t Operadores[%d]: %c ", i, operadores[i]);

}

int main() {
    crearArray();

    crearArbol();

    return 0;
}

當然,打印數組並不是crearArbol的主要目的,但是目前那里什么也沒有,但是我似乎無法弄清楚為什么它會改變。

輸出示例:

有多少個數字?

3

輸入數字:1

輸入運算符:*

輸入數字:2

輸入運算符:/

輸入數字:3

數組:1 * 2/3(從第一個函數crearArray打印的數組)

數組:數字:

Numeros [0]:1

Numeros [1]:0

Numeros [2]:2

運營商:

Operadores [0]:

Operadores [1]:*(第二個函數crearArbol中的打印數組值)

在此先感謝您的幫助!

您正在數組的邊界之外書寫和讀取。 您正在使用i交替索引numerosoperadores ,但這意味着對於任何一個數組, i總是兩倍大。 您需要將i除以2才能將其轉換為可用索引。

num += (num - 1);

for (i = 0; i < num; i++) {

    if(i % 2 == 0 || i == 0) {
        printf("\t\nEnter a number: ");
        scanf("%d", &numeros[i / 2]);
    }
    else {
        fflush(stdin);
        printf("\t\nEnter an operator: ");
        scanf("%c", &operadores[i / 2]);
    }

}

而不是將num加倍,然后根據i是否可被2整除來交替訪問兩個數組,我將按原樣保留num ,而是在每次循環迭代時訪問兩個數組。

for (i = 0; i < num; i++) {

    printf("\t\nEnter a number: ");
    scanf("%d", &numeros[i]);

    if (i < num - 1)
        fflush(stdin);
        printf("\t\nEnter an operator: ");
        scanf("%c", &operadores[i]);
    }

}

出現這種現象的原因是您沒有將項目讀入數組的正確索引中:只有numeros索引numeros ,...具有有效數據,而numeros索引numeros ,... numeros具有有效數據。 oepradores有操作員。 剩余的索引為零,因為您分配了calloc

第一個功能通過打印與讀取的索引相同的索引來掩蓋此問題,而第二個功能通過打印“直接”索引來揭示此問題。

顯然,讀取部分是一個問題,因為它超出了分配的數組的末尾(未定義的行為)。 您可以通過修改讀取循環以一次讀取數字和運算符來解決此問題:

// Do not increment num
for (i = 0; i < num; i++) {
    printf("\t\nEnter a number: ");
    scanf("%d", &numeros[i]);
    if (i == num-1) {
        break; // Last number comes with no operator
    }
    fflush(stdin);
    printf("\t\nEnter an operator: ");
    scanf("%c", &operadores[i]);
}

很明顯,你還需要修改的打印代碼crearArray以匹配的crearArbol

分別地,精神錯誤試圖將兩個單獨的數組視為一個單獨的數組。 查找結構數組的示例。 在單獨的數組上使用單獨的索引變量? 關於復合條件的建議:使用(())而不是依賴先例規則。 我真的不知道您在輸入流中使用fflush。 這也指出缺少錯誤檢查。
通常,了解如何從調試器中轉儲數據。 作為調試思路的來源,請保存代碼,然后進行修改,然后看看會發生什么...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM