How to use Two-Dimensional arrays and pointers

Use Two-Dimensional arrays and pointers


#include <stdio.h>
/*  ww w. ja va 2  s  .  c o m*/
int main(void)
{
  char board[3][3] = {
                       {'1','2','3'},
                       {'4','5','6'},
                       {'7','8','9'}
                     };

  printf("address of board        : %p\n", board);
  printf("address of board[0][0]  : %p\n", &board[0][0]);
  printf("but what is in board[0] : %p\n", board[0]);
  return 0;
}

The code above generates the following result.

Pointer of pointer


#include <stdio.h>
/*from   w w w .j  a  v  a2 s.c o m*/
int main(void)
{
  char board[3][3] = {
                       {'1','2','3'},
                       {'4','5','6'},
                       {'7','8','9'}
                     };

  printf("value of board[0][0] : %c\n", board[0][0]);
  printf("value of *board[0]   : %c\n", *board[0]);
  printf("value of **board     : %c\n", **board);
  return 0;
}

The code above generates the following result.

Loop through an array by using its name as pointer.


#include <stdio.h>
//from w w  w  .  j av a 2  s  . c  om
int main(void)
{
  char board[3][3] = {
                       {'1','2','3'},
                       {'4','5','6'},
                       {'7','8','9'}
                     };


  int i;
  for(i = 0; i < 9; i++)
    printf(" board: %c\n", *(*board + i));
  return 0;
}

The code above generates the following result.





















Home »
  C Language »
    Language Advanced »




File
Function definition
String
Pointer
Structure
Preprocessing