[关闭]
@iwktd981220 2017-11-22T13:47:46.000000Z 字数 1469 阅读 379

二维数组指针表示

笔记


为了帮助德哥的坑爹老师解决这种坑爹问题,我浪费了一个小时。

  1. #include <iostream>
  2. #include <stdio.h>
  3. using namespace std;
  4. int main() {
  5. int a[3][4] = {
  6. {1,2,3,4},
  7. {5,6,7,8},
  8. {9,10,11,12}
  9. }; //for two dimention array it needs two *,or it will output the address
  10. int b[5] = {1,2,3,4,5};
  11. cout<<"1. "<<(*(b+1))<<endl; //should be b[2]
  12. cout<<"2. "<<(**(a+2))<<endl; //take a[2][0]
  13. cout<<"3. "<<(**(a+1+1))<<endl; //equal to the one above
  14. cout<<"4. "<<(**(a+1)+2)<<endl; //take the a[1][2]
  15. cout<<"5. "<<*(*(a+1)+2)<<endl; //the same as the one above
  16. cout<<"7. "<<**(a+1)+2<<endl; //also
  17. //cout<<"9. "<<(*(a+1)+2)+(*(a+2)+1)<<endl; //wtf
  18. cout<<"6. "<<**(a + 1*3 + 2)<<endl; //it is beyond the array
  19. cout<<"8. "<<(*(a+1)+2)<<endl; //take the address of the a[1][2]
  20. printf("%p\n", *(a + 2 + 3)); //it should be out of the array
  21. printf("%d\n", (*(a+2))[3]); //it is a[2][3]
  22. cout<<"12. "<<*(*(a+2)+3)<<endl; //the same as above
  23. cout<<"9. "<<*a<<endl;
  24. printf("%p\n", a);
  25. cout<<"10. "<<a<<endl;
  26. printf("%p\n\n", &a);
  27. cout<<"13. "<<**a+1<<endl; //从这一个系列看出来
  28. printf("%p\n", *a+1);
  29. cout<<"14. "<<**a+2<<endl;
  30. printf("%p\n", *a+2);
  31. cout<<"15. "<<**a+3<<endl;
  32. printf("%p\n\n", *a+3);
  33. cout<<"16. "<<**(a+1)<<endl; //这是另外一个系列
  34. printf("%p\n", *(a+1));
  35. cout<<"17. "<<**(a+2)<<endl;
  36. printf("%p\n\n", *(a+2));
  37. cout<<"11. "<<**a<<endl;
  38. cout<<"12. "<<**(a+2)+3<<endl; //'+' is priorer than '*'
  39. return 0;
  40. }

关键摘要:
1. 结论:a才表示a[0][0],a表示a[0][0]的地址,a表示第一行的地址
2. (暂略等价,由上码可推得)
3. 注意维度与指针数,多少维就应该有多少个‘
’来解地址
4. 到时候总结一下这个*数量和a之间的关系
5. 还有,应该是每一列的元素是连续的。但是解地址后加上的就是相同的行,不同的数字了(好模糊,到时候看着总结吧)
6. *的优先级比较低,故先是进行加法运算,然后再进行解地址。
(a+2)+3 显示的是a[2][4]
7. 对于同一行,也就是同一个大括号下,他们的地址是连续的;而不同行,他们的地址相差倒数第二位?

一个相关的链接:link

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注