Tôi có một ví dụ liên quan đến một con trỏ đến một mảng 2D. Ai đó có thể giúp tôi hiểu những gì đang xảy ra trong ví dụ này?Mảng và con trỏ 2D trong C - cách truy cập các phần tử?
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;
}
Đây có phải là [thẻ: bài tập về nhà] không? – Alexander
Yup! Tôi đã tham gia một khóa học CS giới thiệu và đây chỉ là một ví dụ được đưa ra từ các bài giảng của tôi. – shafools