名稱:二維數組的幾種表示方法
說明:常用的有以下幾種二維數組的表示方法:
#include<iostream>using namespace std;int main(){ int a[3][3] = {{0,1,2},{3,4,5},{6,7,8}}; int b[9] = {0,1,2,3,4,5,6,7,8}; int *c[3]; int **p; cout<<"普通二維數組表示"<<endl; for(int i = 0;i<3;++i) { for(int j = 0;j<3;++j) { cout<<a[i][j]<<"("<<&a[i][j]<<")"<<" "; //括號里是對應元素的地址 //cout<<*(*(a+i)+j)<<" "; } cout<<endl; } cout<<"普通一維數組表示"<<endl; for(int i = 0;i<3;++i) { for(int j = 0;j<3;++j) { cout<<b[i*3+j]<<"("<<&b[i*3+j]<<")"<<" "; } cout<<endl; } cout<<"指針數組表示:"<<endl; for(int i = 0;i<3;++i) { c[i] = *(a+i); //c[i]指向a數組的第i行首地址 for(int j = 0;j<3;++j) { cout<<c[i][j]<<"("<<&c[i][j]<<")"<<" "; } cout<<endl; } cout<<"指針變量表示"<<endl; p = c; //p為指向指針的指針,將指針數組c賦給指針變量p for(int i = 0;i<3;++i) { for(int j = 0;j<3;++j) { cout<<p[i][j]<<"("<<&p[i][j]<<")"<<" "; } cout<<endl; } return 0;}
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林網的支持。如果你想了解更多相關內容請查看下面相關鏈接
新聞熱點
疑難解答
圖片精選