For school (yes, school projects) I need to adapt one C program... I need to make an array with values from a txt file (that I think it was correctly done). Now I wanted to print the values, and that's the problem! I tried many ways but I'm always seeing memory adress. Here's the code:
int* init_dados(char *nome,int *m, int *n, int *iter)
{
FILE *f;
int *p, *q;
int i, j,k,contador=0,lixo=0,aux=0,flag=0;
f=fopen(nome, "r");
if(!f)
{
printf("Erro no acesso ao ficheiro dos dados\n");
exit(1);
}
fscanf(f, " %d %d", m,n);
p = malloc(sizeof(int)*(*m)*(*n));
if(!p)
{
printf("Erro na alocacao de memoria\n");
exit(1);
}
q=p;
for (i = 0; i < *m; i++)
{
for (j = 0; j<*n; j++)
{
//se ainda nao leu nada
if (flag == 0)
{
for (contador = 0; contador < *n; contador++)
{
fscanf(f, "%d", &lixo);
}
flag = 1;
break;
}
if (flag == 1)
{
fscanf(f, " %d", &k);
break;
}
for (contador = 0; contador < k; contador++)
{
fscanf(f, " %d", q++);
}
}
}
//PRINTING CODE
for (i = 0; i < *m; i++)
{
printf("\n");
for (j = 0; j < *n; j++)
{
printf("%d ", &q[j]);
q++;
}
}
fclose(f);
return p;
}
Waiting for your thoughts, thanks !
EDIT: @iharob I've changed this:
for (contador = 0; contador < k; contador++)
{
fscanf(f, " %d", q++);
}
and
for (i = 0; i < *m; i++)
{
printf("\n");
for (j = 0; j < *n; j++)
{
printf("%d ", p[j]);
p++;
}
}
and still not working
EDIT2: file:
10 10
1 1 1 1 1 1 1 1 1 1
2
1 8
2
5 6
4
1 2 3 4
1
1
4
1 2 5 8
2
6 10
1
9
4
1 2 3 5
1
8
1
7
print of the result so far:
