C Tutorial/Array/Multi Dimensional Array Pointer — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
(нет различий)

Текущая версия на 10:32, 25 мая 2010

Declaration of outer block and inner block

#include <stdio.h>
int main(void)
{
  int count1 = 1;                         
  do
  {
    int count2 = 0;                       
    ++count2;
    printf("\ncount1 = %d     count2 = %d", count1,count2);
  }while( ++count1 <= 8 );
  /* count2 no longer exists */
  printf("\ncount1 = %d\n", count1);
  return 0;
}
count1 = 1     count2 = 1
     count1 = 2     count2 = 1
     count1 = 3     count2 = 1
     count1 = 4     count2 = 1
     count1 = 5     count2 = 1
     count1 = 6     count2 = 1
     count1 = 7     count2 = 1
     count1 = 8     count2 = 1
     count1 = 9

Get the values in a two-dimensional array through array pointer

#include <stdio.h>
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;
}
board: 1
      board: 2
      board: 3
      board: 4
      board: 5
      board: 6
      board: 7
      board: 8
      board: 9

Get values from multidimensional arrays with pointers

#include <stdio.h>
int main(void)
{
  char board[3][3] = {
                       {"1","2","3"},
                       {"4","5","6"},
                       {"7","8","9"}
                     };
  char *pboard = *board;             /* A pointer to char */
  int i;
  for(i = 0; i < 9; i++)
    printf(" board: %c\n", *(pboard + i));
  return 0;
}
board: 1
      board: 2
      board: 3
      board: 4
      board: 5
      board: 6
      board: 7
      board: 8
      board: 9

Two-Dimensional arrays and pointers

#include <stdio.h>
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;
}
address of board        : 9a377
     address of board[0][0]  : 9a377
     but what is in board[0] : 9a377

Two-Dimensional arrays: pointer of pointer for its element

#include <stdio.h>
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;
}
value of board[0][0] : 1
     value of *board[0]   : 1
     value of **board     : 1