Arrays 在函数中打印二维数组元素会导致分段错误
我创建了一个2D数组,每行接收6个1-60之间的随机不同值Arrays 在函数中打印二维数组元素会导致分段错误,arrays,c,multidimensional-array,segmentation-fault,Arrays,C,Multidimensional Array,Segmentation Fault,我创建了一个2D数组,每行接收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++)
#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]);
}
一块控制台结果
...
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)
除了在注释中已经讨论过的将数组传递给函数的方式外,代码中还有一些其他问题,下面是一个带有注释的更正版本,其中需要修复:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define QTE 50
#define NUM 6
#包括
#包括
#包括
#定义QTE 50
#定义数字6
//如果要访问数组,请将其用作参数,不需要指针
void mostrar(整数数组[][NUM])
{
printf(“Mostrando..\n”);
对于(int p=0;p
int main(无效)
{
int sorteios[QTE][NUM],repeticao[61]={0};
srand(时间(空));
//从索引1开始,会使第一个索引为空,也会弄乱
//在函数中建立索引,因此从索引0开始
对于(int i=0;i0;k--)
{
如果(j==0)
{
打破
}
而(gerado==sorteios[i][k])
{
gerado=rand()%60+1;
}
}
sorteios[i][j]=gerado;
国际原子能机构=国际原子能机构[i][j];
重复[aleatorio]+=1;
}
}
//仅传递数组,如果包含索引,则仅传递元素
//并且sorteios[QTE][NUM]将超出数组的边界
//并且与原始函数参数也不匹配
mostrar(sorteios);
}
您不传递数组,而只传递一个元素,顺便说一句,它超出了有效数组范围。打开编译器的警告。无关:
int repeticao[61]={}代码>不是有效的标准C(它可能是编译器的有效扩展)。为了兼容,使用int repeticao[61]={0}代码>次要问题:在第一个循环中,索引i
应该从0开始。
#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);
}