Tengo un ejemplo que implica un puntero a una matriz 2D. ¿Alguien puede ayudarme a entender lo que está sucediendo en este ejemplo?Array 2D y puntero en C: cómo acceder a los elementos?
int main()
{
int i = 0, j=0, sum0=0, sum1=0;
int data[4][3] = { {23,55,50},{45,38,55},{70,43,45},{34,46,60}};
int *Ptr;
Ptr = *data; //Why is the indirection operator used here?
// Does Ptr = 23 by this assignment?
for (i=0; i<4; i++) {
sum1 = 0;
for (j = 0; j < 3; j++) {
sum1 += data[i][j];
}
if (sum1 > sum0) {
sum0 = sum1;
Ptr = *(data + i); // Seems like this statement makes Ptr
} // point one row below ... what syntax
} // can you use to access columns then?
// Is it possible to use pointer arithmetic
for (i=0; i<3; i++) // to access elements of data[i][j] that
printf("%d\n", Ptr[i]); // are not at j = 0?
return 0;
}
¿Esto es [etiqueta: tarea]? – Alexander
¡Sip! Estaba tomando un curso introductorio de CS y este fue solo un ejemplo dado en mis notas de clase. – shafools