Re: [問題] 請問有辦法讓陣列的index從1開始嗎?

看板C_and_CPP (C/C++)作者 ( )時間16年前 (2009/11/04 22:45), 編輯推噓2(203)
留言5則, 3人參與, 最新討論串2/4 (看更多)
剛剛在書中的程式看到的,不過目前還看不懂他在寫什麼, 大概跑了一下也是抓不到規律,有興趣的人可以看看! 謝謝! float **matrix(long nrl, long nrh, long ncl, long nch) /* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */ { int NR_END = 1; long i, nrow=nrh-nrl+1,ncol=nch-ncl+1; float **m; /* allocate pointers to rows */ m=(float **) malloc((size_t)((nrow+NR_END)*sizeof(float*)));//size_t??? if (!m) nrerror("allocation failure 1 in matrix()"); m += NR_END; m -= nrl; /* allocate rows and set pointers to them */ m[nrl]=(float *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float))); if (!m[nrl]) nrerror("allocation failure 2 in matrix()"); m[nrl] += NR_END; m[nrl] -= ncl; for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol; /* return pointer to array of pointers to rows */ return m; } ※ 引述《HeyScng ( )》之銘言: : 最近在看 Numerical recipes in C : 可是問題來了,裡面的程式碼陣列的指標看起來是從1開始的... : 每個函數又呼叫了書裡面的其他函數,改起來實在非常麻煩 : 但是我只是想跑看看結果而已, : 想問有沒有用什麼方法讓陣列從1開始,然後又能符合 函數參數 的解決方法。 : 以下列舉一個函數原型, : void lfit(float x[], float y[], float sig[], int ndat, float a[], : int ia[], int ma, float **covar, float *chisq, : void (*funcs)(float, float [], int)) : 我的平台是BCB : 謝謝! 感覺是不太可能有 =.=||| -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.169.42.127

11/04 23:12, , 1F
用意在把index的位址往前取,換言之使用m[<nr1][<n1]違規
11/04 23:12, 1F

11/04 23:15, , 2F
size_t作的是cast;自定義的型別,可能代表unsinged long
11/04 23:15, 2F

11/05 00:14, , 3F
很多無意義的寫法 NR_END也沒再變動過, 可以都代換成1
11/05 00:14, 3F

11/05 00:21, , 4F
那叫命名常數 你不怕到處都有magic number就改吧 哈
11/05 00:21, 4F

11/05 01:08, , 5F
老實說寫錯的地方相當多 真的是教科書上的??
11/05 01:08, 5F
文章代碼(AID): #1AyPEKIp (C_and_CPP)
文章代碼(AID): #1AyPEKIp (C_and_CPP)