[問題] 關於陣列初始化的問題

看板C_and_CPP作者 (特命係長摩野仁)時間11年前 (2012/10/28 17:33), 編輯推噓1(105)
留言6則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) codepad 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 我想寫一個初始化矩陣的函式 但是code的結果一直都是Segmentation fault 想請問一下我到底哪邊搞錯了 還是哪邊觀念有問題 請幫忙指正 謝謝 餵入的資料(Input): #include <stdio.h> #define N 5 void init(int a,int (*ma)[a]); int main(void){ int m[N][N]; int (*pt)[N]; pt=m; init(N,pt); int i,j; for(i=0;i<N;i++) for(j=0;j<N;j++){ printf("%d ",m[i][j]); printf("\n");} return 0;} void init(int a,int (*ma)[a]){ int c,d; for (c=0;c<a;c++) for (d=0;d<a;d++) *(*(ma+c)+d)=0;} 預期的正確結果(Expected Output): 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 錯誤結果(Wrong Output): Segmentation fault 程式碼(Code):(請善用置底文網頁, 記得排版) 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 203.66.30.9

10/28 18:16, , 1F
你的 printf('\n'); 裡面引號改一下, '' 改成""
10/28 18:16, 1F

10/28 18:16, , 2F
然後你就會發現你的那個printf("\n");放錯括弧了
10/28 18:16, 2F

10/28 18:28, , 3F
嗯嗯 應該是("\n")這樣才對 感謝 XD
10/28 18:28, 3F
※ 編輯: b9307025 來自: 203.66.30.9 (10/28 19:53)

10/29 00:12, , 4F
為何不 init(m);
10/29 00:12, 4F

10/29 01:45, , 5F
我有想過但是我不知道多維陣列是否能夠直接當arguments
10/29 01:45, 5F

10/29 01:46, , 6F
後來我朋友幫我組譯沒有出現 很奇怪 弄了一整天說 ==
10/29 01:46, 6F
文章代碼(AID): #1GZFlM4P (C_and_CPP)