Re: [語法] 在C++的header如何使用2維陣列?

看板C_and_CPP (C/C++)作者 (SOSOYA)時間16年前 (2009/03/27 20:08), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《dcleft (SOSOYA)》之銘言: : header檔用int a[][]是不予許的 : 所以我改用int **a : 現在想動態宣告一個 3 x 5 的array : 在 C 的 source file 裡面我會這樣用 : a=(int**)malloc(sizeof(int*)*3); : int i; : for(i=0;i<3;i++) : *(a+i)=(int*)malloc(sizeof(int)*5); : 但是 C++ 中要如何定義第一維是3個element? : 試了 a=new int*[3]; 會出現compile error ,除了用vector想不到別的辦法了 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 對不起,我耍笨了,new int*[3] 是可以的 ,可以用下面方式寫 int **a; a=new int*[3]; for(int i=0;i<3;i++) a[i]=new int[5]; for(int i=0;i<3;i++) for(int j=0;j<5;j++) a[i][j]=i*5+j; for(int i=0;i<3;i++) for(int j=0;j<5;j++) cout<<a[i][j]<<" "; 會印出 0 ~ 14 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.99.0.10 ※ 編輯: dcleft 來自: 122.99.0.10 (03/27 20:09)
文章代碼(AID): #19pC7KCC (C_and_CPP)
文章代碼(AID): #19pC7KCC (C_and_CPP)