Re: [問題] 請問有辦法讓陣列的index從1開始嗎?
剛剛在書中的程式看到的,不過目前還看不懂他在寫什麼,
大概跑了一下也是抓不到規律,有興趣的人可以看看! 謝謝!
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
11/04 23:12, 1F
→
11/04 23:15, , 2F
11/04 23:15, 2F
→
11/05 00:14, , 3F
11/05 00:14, 3F
→
11/05 00:21, , 4F
11/05 00:21, 4F
推
11/05 01:08, , 5F
11/05 01:08, 5F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章