Re: [問題] 怎樣把二維陣列傳給另一個function

看板C_and_CPP (C/C++)作者 (CA)時間16年前 (2009/03/14 19:10), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串2/6 (看更多)
※ 引述《y2jj (.)》之銘言: : #include <stdio.h> : #include <stdlib.h> : int x,y; : void sub(int b[x][y]){ : … : } : void main(){ : printf("x:");scanf("%d",&x); : printf("y:");scanf("%d",&y); : int a[x][y]; : sub(a); : } : 錯誤訊息:variable-size type declared outside of any function : 我想讓副程式sub知道陣列維度 : 不知道錯在哪邊 : 請各位前輩指點 謝謝~~~ void sub( int * * matrix, int row_size, int col_size ); int main() { int x, y; scanf( "%d %d", &x, &y ); int * * matrix = calloc( y, sizeof( int * ) ); for( int i = 0; i < y; ++i ) { matrix[i] = calloc( x, sizeof( int ) ); } // do something ... sub( matrix, x, y ); // do something ... for( int i = 0; i < y; ++i ) { free( matrix[i] ); } free( matrix ); return 0; } -- 自High筆記(半荒廢) http://legnaleurc.blogspot.com/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 211.74.131.175

03/14 22:43, , 1F
感謝~~
03/14 22:43, 1F
文章代碼(AID): #19kv2p7i (C_and_CPP)
文章代碼(AID): #19kv2p7i (C_and_CPP)