【问题标题】:Printing 2D array elements within a function is causing a segmentation fault在函数中打印二维数组元素会导致分段错误
【发布时间】:2021-06-19 21:28:48
【问题描述】:

我创建了一个二维数组,每行接收 6 个 1-60 之间的随机不同值。

#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 函数中有一个 for 循环,则此代码本身可以工作,但使用该函数执行会导致分段错误(核心转储)。

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)

【问题讨论】:

  • 您没有传递mostrar 数组,而只传递了一个元素,顺便说一句,它超出了有效数组范围。打开编译器的警告。
  • 无关:int repeticao[61] = {}; 不是有效的标准 C(它可能是您的编译器的有效扩展)。为了兼容性,请使用int repeticao[61] = {0};
  • 小问题:在第一个循环中,索引i应该从0开始。

标签: arrays c multidimensional-array segmentation-fault


【解决方案1】:

除了将数组传递给函数的方式(已经在 cmets 中讨论过)之外,您的代码中还有一些其他问题,这里是一个带有 cmets 的更正版本,需要修复:

#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

【讨论】:

    猜你喜欢
    • 2012-09-09
    • 2019-08-03
    • 1970-01-01
    • 2012-02-25
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    相关资源
    最近更新 更多