程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 學點C語言(22):數據類型 - 多維數組與指針

學點C語言(22):數據類型 - 多維數組與指針

編輯:關於C語言

1. 關於數組的首地址:

#include <stdio.h>
int main(void)
{
char cs[2][3] = {
{'A','B','C'},
{'D','E','F'}
};
char *p1,*p2,*p3,*p4;
p1 = p2 = p3 = p4 = NULL;
/* 下面四個指針都是指向了同一個地址 */
p1 = &cs[0][0]; /* 這個最好理解 */
p2 = &cs[0];
p3 = &cs;
p4 = cs; /* 這個最方便 */
printf("%p\n%p\n%p\n%p\n", p1, p2, p3, p4); /* 顯示地址 */
printf("\n%c %c %c %c\n", *p1, *p2, *p3, *p4); /* 顯示內容 */
getchar();
return 0;
}

2. 數組其他元素的地址:

例子中,數組的元素在內存中應該是這樣排列的:

[0][0] [0][1] [0][2] [1][0] [1][1] [1][2]

下面是通過指針的方式獲取數組的第三個元素:

#include <stdio.h>

int main(void)
{
  int nums[2][3] = {
            {11,12,13},
            {21,22,23}
           };
  int *p1,*p2;
  p1 = p2 = NULL;

  p1 = &nums[0][2];

  p2 = nums;
  p2 = p2 + 2;
//  p2 = (int *)nums + 2; /* 或者用這一句替換上面兩行 */

  printf("%d,%d\n",*p1,*p2);

  getchar();
  return 0;
}

3. 遍歷數組的普通方法:

#include <stdio.h>

int main(void)
{
  int nums[2][3] = {
            {11,12,13},
            {21,22,23}
           };
  int i,j;
  for (i = 0; i < 2; i++) {
    for (j = 0; j < 3; j++) {
      printf("%d\n",nums[i][j]);
    }
  }

  getchar();
  return 0;
}

4. 通過指針遍歷數組:

#include <stdio.h>

int main(void)
{
  int nums[2][3] = {
            {11,12,13},
            {21,22,23}
           };
  int *p = nums;

  int i;
  for (i = 0; i < 6; i++) {
    printf("%d\n",*(p+i));
  }

  getchar();
  return 0;
}

#include <stdio.h>

int main(void)
{
  char cs[2][3] = {
           {'A','B','C'},
           {'D','E','F'}
          };
  char *p = cs;

  unsigned i;
  for (i = 0; i < sizeof cs / sizeof cs[0][0]; i++) {
    printf("%c\n",*p++);
  }

  getchar();
  return 0;
}

#include <stdio.h>

int main(void)
{
  char cs[2][3] = {
           {'A','B','C'},
           {'D','E','F'}
          };
  int i;
  for (i = 0; i < 6; i++) {
     printf("%c\n",*(*cs + i)); // *cs是什麼?看下例
  }

  getchar();
  return 0;
}

5. 再探數組的指針地址:

#include <stdio.h>

int main(void)
{
  char cs[2][3] = {
           {'A','B','C'},
           {'D','E','F'}
          };

  //在本例中(二維數組)
   // cs是指向數組cs[0]的地址
   // *cs是指向cs[0][0]的地址
  printf("%p,%p\n",cs,*cs);

  // **cs 指向 cs[0][0] 的值
  printf("%c,%c\n",**cs,cs[0][0]);

  getchar();
  return 0;
}

6. 使用指針遍歷三維數組:

#include <stdio.h>

int main(void)
{
  char cs[2][2][3] = {
             {
              {'A','B','C'},
              {'D','E','F'}
             },
             {
              {'1','2','3'},
              {'4','5','6'}
             }
            };
  int i;
  int count = sizeof cs / sizeof cs[0][0][0];

  for (i = 0; i < count; i++) {
    printf("%c\n",*(**cs + i));
  }

  getchar();
  return 0;
}

7. 遍歷多維數組還是用指針變量更容易理解:

#include <stdio.h>

int main(void)
{
  char cs[2][2][3] = {
             {
              {'A','B','C'},
              {'D','E','F'}
             },
             {
              {'1','2','3'},
              {'4','5','6'}
             }
            };
  char *p = (char *)cs; /* 相對上面的例子,這裡加了類型轉換;這樣編譯器就沒有提示了 */

  int i;
  int count = sizeof cs / sizeof cs[0][0][0];
  for (i = 0; i < count; i++) {
    printf("%c\n",*p++);
  }

  getchar();
  return 0;
}

返回“學點C語言 - 目錄”

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved