Multiple Dimension Array
#include <stdio.h> int main() { int a[3][4] = {{1,2,3,4},{9,10,11,12},{13,14,15,16}}; printf("%d\n", a[0][0]); printf("%d\n", a[2][3]); printf("%d\n", *(*(a+2)+3)); printf("%d\n", *(*a + 2*4 + 3)); // a is pointer to pointer }
Output:
1
16
16
16
Note:
- for int a[m][n], we can use *(*(a+x) + y) and *(*a + x*n + y) to access a[x][y]. In both methods, we need to dereference twice.
- If we pass the array a[][] to a function as parameter, it will be like we passing in the pointer to pointer a. However we need to denote the n, because therefore compiler know how to translate it when we write a[x][y].
char *color_display_buffer = (char*) 0xB8000; color_display_buffer[2*(80*row+col)] = 'A';
can be changed to
typedef char ONE_CELL[2]; typedef ONE_CELL ONE_ROW[80]; ONE_ROW *color_display_buffer = (ONE_ROW*) 0xB8000 color_display_biffer[row][col][0] = 'A';
page revision: 11, last edited: 19 Jul 2011 05:06