C - Using pointer to access multidimensional arrays

Description

Using pointer to access multidimensional arrays

Demo

#include <stdio.h>

int main(void)
{
  char board[3][3] = {
    { '1','2','3' },
    { '4','5','6' },
    { '7','8','9' }
  };//  www  . j a v  a 2s  .  co m

  char *pboard = *board;             // A pointer to char
  for (int i = 0; i < 9; ++i)
    printf(" board: %c\n", *(pboard + i));

  return 0;
}

Result

The following code initializes pboard with the address of the first element of the array.

And then you use normal pointer arithmetic to move through the array:

char *pboard = *board;               // A pointer to char
for(int i = 0 ; i < 9 ; ++i)
      printf(" board: %c\n", *(pboard + i));

Related Topic