簡體   English   中英

在 function 中打印二維數組元素會導致分段錯誤

[英]Printing 2D array elements within a function is causing a segmentation fault

我創建了一個二維數組,每行接收 1-60 之間的 6 個隨機不同值。

#define QTE 50
#define NUM 6

void mostrar();

int main(void)
{
    int sorteios[QTE][NUM], repeticao[61] = {};
    srand(time(NULL));

    for (int i = 1; i < QTE; i++)
    {
        for (int j = 0; j < 6; j++)
        {
            int gerado = rand() % 60 + 1;

            for (int k = j; k > 0; k--)
            {
                if (j == 0)
                {
                    break;
                }
                while (gerado == sorteios[i][k])
                {
                    gerado = rand() % 60 + 1;
                }
            }
            sorteios[i][j] = gerado;
            int aleatorio = sorteios[i][j];
            repeticao[aleatorio] += 1;
        }
        printf("Sequência %04d:\t %02d\t%02d\t%02d\t%02d\t%02d\t%02d\n", i, sorteios[i][0], sorteios[i][1], sorteios[i][2], sorteios[i][3], sorteios[i][4], sorteios[i][5]);
    }
    mostrar(sorteios[QTE][NUM]);
}

如果main function 中有一個 for 循環,但使用 function 執行導致分段錯誤(核心轉儲),則此代碼本身正在工作。

void mostrar(int *array[QTE][NUM])
{
    printf("Mostrando..\n");
    for (int p = 0; p < QTE; p++)
    {
        printf("Sequência %04d:\t %02d\t%02d\t%02d\t%02d\t%02d\t%02d\n", p, *array[p][0], *array[p][1], *array[p][2], *array[p][3], *array[p][4], *array[p][5]);
    }
}

控制台結果的一部分..

...
Sequência 0043:  35 59  08  31  16  40
Sequência 0044:  26 47  27  52  32  08
Sequência 0045:  35 34  26  35  31  14
Sequência 0046:  07 44  13  22  35  46
Sequência 0047:  50 17  16  53  49  29
Sequência 0048:  27 39  37  50  10  44
Sequência 0049:  29 35  30  55  18  53
Mostrando..
Segmentation fault (core dumped)

除了將數組傳遞給 function 的方式(已在評論中討論)之外,您的代碼中還有一些其他問題,這里是一個更正版本,其中包含需要修復的注釋:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define QTE 50
#define NUM 6
// if you want the access the array, jut use that as an argument, no pointer needed
void mostrar(int array[][NUM]) 
{
    printf("Mostrando..\n");
    for (int p = 0; p < QTE; p++)
    {
        // correcting the dereference to match the argument...
        printf("Sequência %04d:\t %02d\t%02d\t%02d\t%02d\t%02d\t%02d\n", p, array[p][0], array[p][1], array[p][2], array[p][3], array[p][4], array[p][5]);
    }
}
int main(void)
{
    int sorteios[QTE][NUM], repeticao[61] = {0};
    srand(time(NULL));
     
    // beginning at index 1 would leave the first index empty, also messing up the 
    // indexing in the function, so start at index 0
    for (int i = 0; i < QTE; i++) 
    {
        for (int j = 0; j < 6; j++)
        {
            int gerado = rand() % 60 + 1;

            for (int k = j; k > 0; k--)
            {
                if (j == 0)
                {
                    break;
                }
                while (gerado == sorteios[i][k])
                {
                    gerado = rand() % 60 + 1;
                }
            }
            sorteios[i][j] = gerado;
            int aleatorio = sorteios[i][j];
            repeticao[aleatorio] += 1;
        }
    }
    // pass only the array, if you include indexes you are just passing an element 
    // of the array, and sorteios[QTE][NUM] would be outside of the bounds of the array
    // and wouldn't match the original function argument either
    mostrar(sorteios);
}

現場演示

暫無
暫無

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

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