Chinaunix首页 | 论坛 | 博客
  • 博客访问: 42909
  • 博文数量: 13
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 145
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-19 10:39
文章分类

全部博文(13)

文章存档

2016年(3)

2015年(5)

2014年(5)

我的朋友

分类: C/C++

2015-02-24 01:16:38


点击(此处)折叠或打开

  1. #include <iostream>

  2. using namespace std;

  3. void printArray1_1(int count,int ary[])
  4. {
  5.     for(int i = 0; i < count; ++i)
  6.     {
  7.         cout << "ary1[" << i << "] = " << ary[i] << endl;
  8.     }
  9. }

  10. void printArray1_2(int count,int* ary)
  11. {
  12.     for(int i = 0; i < count; ++i)
  13.     {
  14.         cout << "ary1[" << i << "] = " << *(ary + i) << endl;
  15.     }
  16. }

  17. void doubleArray1_1(int count,int ary[])
  18. {
  19.     for(int i = 0; i < count; ++i)
  20.     {
  21.         // int ary[] <--> int* ary
  22.         // !!!! Passed function-arument by pointer
  23.         // !!!! So can change the value of the pointer
  24.         ary[i] *= 2;
  25.     }
  26. }

  27. void doubleArray1_2(int count,int* ary)
  28. {
  29.     for(int i = 0; i < count; ++i)
  30.     {
  31.         // int ary[] <--> int* ary
  32.         // !!!! Passed function-arument by pointer
  33.         // !!!! So can change the value of the pointer
  34.         *(ary + i) *= 2;
  35.     }
  36. }

  37. int main(int argc, char* argv[])
  38. {
  39.     int ary1[3] = { 1, 3, 5 };
  40.     int ary2[3][5] = {
  41.                      { 1, 3, 5, 7,    9 },    // row 0
  42.                      { 11, 33, 55, 77, 99 },    // row 1
  43.                      { 111, 333, 555, 777,999 },    // row 2
  44.                      };

  45.     //
  46.     // 1-dimension Array Test
  47.     //
  48.     
  49.     // ary1 is 1-dimesion Array
  50.     // ary1 <--> &ary1[0]    , is a pointer point to int number
  51.     cout << " ary1 = " << ary1 << endl;
  52.     cout << " (ary1 + 0) = " << (ary1 + 0) << endl;
  53.     // (ary1 + 1) <--> &ary1[1]
  54.     cout << " (ary1 + 1) = " << (ary1 + 1) << endl;
  55.     // delta = sizeof(int) = 4(bytes)
  56.     // get the real number value in the address
  57.     cout << " *ary1 = " << *ary1 << endl;         // *&ary1[0] --> ary1[0] = 1
  58.     cout << "*(ary1 + 0) = " << *(ary1 + 0) << endl; // ary1[0] <--> *(ary1 + 0) = 1
  59.     cout << "*(ary1 + 1) = " << *(ary1 + 1) << endl; // ary1[1] <--> *(ary1 + 1) = 3

  60.     cout <<"-------------------------------------------------------" << endl;

  61.     //
  62.     // 2-dimension Array Test
  63.     //
  64.     
  65.     // ary2 <--> &ary2[0]
  66.     // pointer to { 1, 3, 5, 7,    9 },    // row 0 , 1-dim array's address
  67.     cout << " ary2 = " << ary2 << endl;
  68.     // *ary2 --> *&ary2[0] --> ary2[0] --> &ary2[0][0]
  69.     // because the ary2[0] is an 1-dim array's name
  70.     cout << " *ary2 = " << *ary2 << endl;
  71.     cout << " (ary2 + 0) = " << (ary2 + 0) << endl;
  72.     // ary2 <--> &ary2[0] ,
  73.     // so ary2 + 1 = &ary2[0] + 1,
  74.     // it means offset the 1-dim array's pointer(&ary2[0]) +1 step,
  75.     // so it then pointer to ary2[1] = &ary2[1]
  76.     // pointer to { 11, 33, 55, 77,    99 },    // row 1 , 1-dim array's address
  77.     cout << " (ary2 + 1) = " << (ary2 + 1) << endl;
  78.     // delta from (ary2 + 1) to (ary2 + 0) is 5*4 , 5 int space


  79.     // *(ary2 + 0) --> *(ary2) --> *ary2 --> *&ary2[0] --> ary2[0] --> &ary2[0][0] --> pointer to 1
  80.     cout << "*(ary2 + 0) = " << *(ary2 + 0) << endl;
  81.     // *(ary2 + 1) --> *(ary2 + 1) --> *ary2[1] --> *&ary2[1] --> ary2[1] --> &ary2[1][0] --> pointer to 11
  82.     cout << "*(ary2 + 1) = " << *(ary2 + 1) << endl;

  83.     cout <<"-------------------------------------------------------" << endl;
  84.     // *(ary2 + 0) + 1 --> &ary2[0][0] offset 1 int space
  85.     cout << "*(ary2 + 0) + 1 = " << (*(ary2 + 0) + 1) << endl;
  86.     cout << " &ary2[0][1] = " << &ary2[0][1] << endl;
  87.     // *(*(ary2 + 0) + 0) --> *&ary2[0][0] --> ary2[0][0] --> 1
  88.     cout << "*(*(ary2 + 0) + 0) = " << *(*(ary2 + 0) + 0) << endl;
  89.     // --> 3
  90.     cout << "*(*(ary2 + 0) + 1) = " << *(*(ary2 + 0) + 1) << endl;
  91.     // *(*(ary2 + 1) + 0) --> *&ary2[1][0] --> ary2[1][0] --> 11
  92.     cout << "*(*(ary2 + 1) + 0) = " << *(*(ary2 + 1) + 0) << endl;
  93.     // --> 33
  94.     cout << "*(*(ary2 + 1) + 1) = " << *(*(ary2 + 1) + 1) << endl;

  95.     cout <<"-------------------------------------------------------" << endl;
  96.     // Print Array
  97.     printArray1_1(3,ary1);
  98.     printArray1_2(3,ary1);

  99.     // Double Array
  100.     doubleArray1_1(3,ary1);
  101.     cout << "After Double Value of Array1 : " << endl;
  102.     printArray1_1(3,ary1);

  103.     doubleArray1_2(3,ary1);
  104.     cout << "After twice Double Value of Array1 : " << endl;
  105.     printArray1_2(3,ary1);
  106.     return 0;
  107. }


阅读(1061) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~