0
I intend to use this function to dynamically allocate and populate an array, of indeterminate element size, where each row has only two elements, that is, an ordered pair.
int **lePares()
{
int **pares = (int **) malloc (sizeof(int *)); // Aloco uma "matriz" temporária com um par ordenado.
int i = 0, j;
while (1)
{
j = 0;
pares[i] = (int *) malloc (sizeof(int) * 2);
while (j < 2);
{
printf("elemento");
scanf("%i", &pares[i][j]);
j++;
}
if (pares[i][0] == EOF) // Se o valor for o ultimo a ser lido, ele sai do loop.
{
break;
}
i++; // Conta a quantos elementos foram lidos.
pares = (int **) realloc (pares, (i + 2) * sizeof(int *)); // Realoca a matriz com a quantidade correta de elementos lidos.
}
return pares;
}
Could you post the error and where it happens? By title this is some pointer that is losing the reference, which causes memory segmentation. See the answer Confusion in the question in this other post, maybe she’ll help you.
– bruno101
Instead of
scanf("%i", &pares[i][j]);
, doscanf("%d", &pares[i][j]);
.– Marcelo Shiniti Uchimura