2
Hello, I was trying to do an exercise in which you asked to print the address of each position of the matrix using pointer and I am in doubt if it is correct. I searched some videos on youtube and only found explanation for one-dimensional matrices.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float matrix[3][3];
float **xmatrix= matrix;
printf("Endereços de memória\n\n");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
printf("Membro [%d] [%d]\n\n",i,j);
printf("%d\n\n\n",xmatrix+j+i);\\aqui fiquei em dúvida em com faria para imprimir os próximos
\\membros
}
}
return 0;
}
And if I’m right ,because some of the addresses are repeated.
Thanks in advance.
it is repeating because you are adding the address + i + j, note for example in the cases [0][1] and [1][0] are equal pq x+1+0 = x+0+1 , in my view the easiest way to get the pointer addresses would be with a & in Matrix, ex: &Matrix[0][1]
– Lucas Miranda
If you want to use the way it is in your program the correct formula is:
xmatrix+j+(2*i)
where this 2 is the row size of your matrix.– anonimo