[問題] 不會用Memory allocation和指標
各位大大好
小弟我是C語言新手
一直搞不懂pointer &pointer's pointer
請問一下喔
int size, i; double **m, *iptr;
size = nr * nc;
m = ( double ** ) malloc ( nr * sizeof ( double * ) );
if ( m == NULL )
{ printf ("\nerror 1\n"); exit ( 0 ); }
iptr = ( double * ) malloc ( size * sizeof ( double ) );
if ( iptr == NULL )
{ printf ("\nerror 2\n"); exit ( 0 ); }
for ( i = 0; i < nr; i++ ) { m [i] = iptr; iptr += nc; }
return m;
以上是我要安排一個A[i][j]矩陣
所需要的公式
可不可以請問一下
1.(double **)是什麼意思?
我前面用的是grade 為什麼後面要加(double **)
不是**grade 才需要指到指標的指標的記憶位置??
2.不是grade->*grade(*iptr)->**grade???
為什麼先在grade 安排nr個double *
而在iptr安排size個double
3.照指標的指向位置
不是應該是grade要指向nr個(double*) *iptr指向nc個(double**)嗎
所以grade應該才要有nr*nc個記憶位置嗎?
不應該是這樣嗎→
int size, i; double **m, *iptr;
size = nr * nc;
**m = ( double ** ) malloc ( size * sizeof ( double * ) );
if ( m == NULL )
{ printf ("\nerror 1\n"); exit ( 0 ); }
*iptr = ( double * ) malloc ( nc * sizeof ( double ) );
if ( iptr == NULL )
{ printf ("\nerror 2\n"); exit ( 0 ); }
for ( i = 0; i < nr; i++ ) { m [i] = iptr; iptr += nc; }
return m;
各位大大
幫幫小弟吧
我真的不懂>"<
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.7.59
推
05/12 02:17, , 1F
05/12 02:17, 1F
→
05/12 02:17, , 2F
05/12 02:17, 2F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章