Re: [問題] 怎樣把二維陣列傳給另一個function
※ 引述《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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 6 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章