struct由傳pointer改為傳reference |
缺席
|
maplefog
一般會員 發表:16 回覆:24 積分:13 註冊:2008-11-08 發送簡訊給我 |
最近在練習C 的程式,發現call by reference還滿好用的 有些時候比傳指標更好理解 所以想把之前寫的矩陣轉置程式改為傳reference 但是錯誤一堆... 可以幫忙解惑嗎 當struct 遇上reference 我就疑惑了 感謝!! 這是一開始傳pointer方式,可以正常執行 [code cpp] #include typedef struct { int rows; int cols; double *table; } Matrix; void matrix_clear(Matrix *matrix); Matrix *matrix_create(int rows,int cols); void matrix_print(Matrix *matrix); Matrix *matrix_value(int rows,int cols,double *data); Matrix *transport(Matrix *matrix); using namespace std; int main() { double input[3][4] = {{1,1,1,5},{2,2,2,5},{3,3,3,5}}; int rows = sizeof(input)/sizeof(input[0]); int cols = sizeof(input[0])/sizeof(input[0][0]); Matrix *data = matrix_value(rows,cols,*input); Matrix *tdata = transport(data); matrix_print(data); matrix_print(tdata); return 0; } //=================================================== //所有值清除為0 //=================================================== void matrix_clear(Matrix *matrix) { int i; double *ptr; int total = matrix->rows*matrix->cols; ptr = matrix->table; for(i=0;i!=total; i) *(ptr ) = 0; } //=================================================== //配置矩陣空間 //=================================================== Matrix *matrix_create(int rows,int cols) { Matrix *out; out = (Matrix *)malloc(sizeof(Matrix)); out->rows = rows; out->cols = cols; out->table = (double *)malloc(sizeof(double)*rows*cols); matrix_clear(out); return out; } //=================================================== //顯示矩陣內容 //=================================================== void matrix_print(Matrix *matrix) { int i,j; double *ptr; ptr = matrix->table; for(i=0;i!=matrix->rows; i) for(j=0;j!=matrix->cols; j) if(j==matrix->cols-1) cout << *(ptr ) << "\n"; else cout << *(ptr ) << ","; } //=================================================== //初始值 //=================================================== Matrix *matrix_value(int rows,int cols,double *data) { int i; double *ptr; Matrix *out = matrix_create(rows,cols); ptr = out->table; int total = rows*cols; for(i=0;i!=total; i) *(ptr ) = *(data ); return out; } //=================================================== //矩陣轉置 //=================================================== Matrix *transport(Matrix *in) { int i,j; double *inptr,*outptr; Matrix *out = matrix_create(in->cols,in->rows); inptr = in->table; outptr = out->table; for(i=0;i!=in->rows; i) for(j=0;j!=in->cols; j) *(outptr j*in->rows i) = *(inptr ); return out; } //=================================================== [/code] 以下是我改為傳reference的,錯誤一堆... [code cpp] #include typedef struct { int rows; int cols; double *table; } Matrix; void matrix_clear(Matrix &matrix); Matrix &matrix_create(int rows,int cols); void matrix_print(Matrix &matrix); Matrix &matrix_value(int rows,int cols,double *data); Matrix &transport(Matrix &matrix); using namespace std; int main() { double input[3][4] = {{1,1,1,5},{2,2,2,5},{3,3,3,5}}; int rows = sizeof(input)/sizeof(input[0]); int cols = sizeof(input[0])/sizeof(input[0][0]); Matrix &data = matrix_value(rows,cols,*input); Matrix &tdata = transport(data); matrix_print(data); matrix_print(tdata); return 0; } //=================================================== //所有值清除為0 //=================================================== void matrix_clear(Matrix &matrix) { int i; double *ptr; int total = matrix.rows*matrix.cols; ptr = matrix.table; for(i=0;i!=total; i) *(ptr ) = 0; } //=================================================== //配置矩陣空間 //=================================================== Matrix &matrix_create(int rows,int cols) { Matrix *out; out = (Matrix *)malloc(sizeof(Matrix)); out.rows = rows; out.cols = cols; out.table = (double *)malloc(sizeof(double)*rows*cols); matrix_clear(out); return out; } //=================================================== //顯示矩陣內容 //=================================================== void matrix_print(Matrix &matrix) { int i,j; double *ptr; ptr = matrix.table; for(i=0;i!=matrix.rows; i) for(j=0;j!=matrix.cols; j) if(j==matrix.cols-1) cout << *(ptr ) << "\n"; else cout << *(ptr ) << ","; } //=================================================== //初始值 //=================================================== Matrix &matrix_value(int rows,int cols,double &data) { int i; double *ptr; Matrix *out = matrix_create(rows,cols); ptr = out.table; int total = rows*cols; for(i=0;i!=total; i) *(ptr ) = *(data ); return out; } //=================================================== //矩陣轉置 //=================================================== Matrix &transport(Matrix &in) { int i,j; double *inptr,*outptr; Matrix *out = matrix_create(in.cols,in.rows); inptr = in->table; outptr = out->table; for(i=0;i!=in.rows; i) for(j=0;j!=in.cols; j) *(outptr j*in.rows i) = *(inptr ); return out; } //=================================================== [/code] |
maplefog
一般會員 發表:16 回覆:24 積分:13 註冊:2008-11-08 發送簡訊給我 |
突然在網路上看到一篇類似的 照著修改就可以執行了。 [code cpp] #include typedef struct matrix* Matrix; struct matrix { int rows; int cols; double *table; }; void matrix_clear(Matrix &matrix); Matrix matrix_create(int rows,int cols); void matrix_print(Matrix &matrix); Matrix matrix_value(int rows,int cols,double *data); Matrix transport(Matrix &matrix); using namespace std; int main() { double input[3][4] = {{1,1,1,5},{2,2,2,5},{3,3,3,5}}; int rows = sizeof(input)/sizeof(input[0]); int cols = sizeof(input[0])/sizeof(input[0][0]); Matrix data = matrix_value(rows,cols,*input); Matrix tdata = transport(data); matrix_print(data); matrix_print(tdata); return 0; } //=================================================== //所有值清除為0 //=================================================== void matrix_clear(Matrix &matrix) { int i; double *ptr; int total = matrix->rows*matrix->cols; ptr = matrix->table; for(i=0;i!=total; i) *(ptr ) = 0; } //=================================================== //配置矩陣空間 //=================================================== Matrix matrix_create(int rows,int cols) { Matrix out; out = (Matrix)malloc(sizeof(struct matrix)); out->rows = rows; out->cols = cols; out->table = (double *)malloc(sizeof(double)*rows*cols); matrix_clear(out); return out; } //=================================================== //顯示矩陣內容 //=================================================== void matrix_print(Matrix &matrix) { int i,j; double *ptr; ptr = matrix->table; for(i=0;i!=matrix->rows; i) for(j=0;j!=matrix->cols; j) if(j==matrix->cols-1) cout << *(ptr ) << "\n"; else cout << *(ptr ) << ","; } //=================================================== //初始值 //=================================================== Matrix matrix_value(int rows,int cols,double *data) { int i; double *ptr; Matrix out = matrix_create(rows,cols); ptr = out->table; int total = rows*cols; for(i=0;i!=total; i) *(ptr ) = *(data ); return out; } //=================================================== //矩陣轉置 //=================================================== Matrix transport(Matrix &in) { int i,j; double *inptr,*outptr; Matrix out = matrix_create(in->cols,in->rows); inptr = in->table; outptr = out->table; for(i=0;i!=in->rows; i) for(j=0;j!=in->cols; j) *(outptr j*in->rows i) = *(inptr ); return out; } //=================================================== [/code] |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |