简体   繁体   English

在 function 中打印二维数组元素会导致分段错误

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

I created an 2D array, each line receiving 6 random different values between 1-60.我创建了一个二维数组,每行接收 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]);
}

This code itself is working if there's a for loop inside the main function but using the function to execute causes segmentation fault(core dumped).如果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]);
    }
}

A piece of the console result..控制台结果的一部分..

...
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)

Aside from the way you pass the array to the function, already discussed in the comments, there are some other issues in your code, here is a corrected version with comments where fixes are needed:除了将数组传递给 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);
}

Live demo现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM