作者SNSDpk5566 (5566 超強)
看板C_and_CPP
標題[問題] 一維指標指向二維陣列
時間Mon Jan 9 18:38:32 2012
請問一下我想要用一維指標指向二維陣列
CODE如下
雖然結果是對的
為什麼會有一個warning
warning 'int *' differs in levels of indirection from 'int (*)[8]'
我應該要怎麼修正比較好呢
感謝各位
#include <stdio.h>
void main()
{
int array2D[5][8] = {{1,2,3,4,5,6,7,8},
{11,12,13,14,15,16,17,18},
{21,22,23,24,25,26,27,28},
{31,32,33,34,35,36,37,38},
{41,42,43,44,45,46,47,48},
};
int *ptr;
int i,j;
ptr = array2D;
for(i = 0; i<5; i++)
{
for(j = 0; j<8; j++)
{
printf("%d\n",*(ptr+8*i+j));
}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.210.106.78
→ loveme00835:ptr = &array2D[0][0]; 01/09 18:43
→ eight0:ptr = (int*)array2D; 01/09 20:44
→ DEATHX:ptr = array2D[0] 01/09 20:45
→ mythnc:int (*ptr)[8]; 01/09 20:52
→ mythnc:不過這樣就要寫成*(*(pt + i) + j) 嗯…… 01/09 20:56
→ SNSDpk5566:感謝大大問題解了~ 01/10 11:44