[問題] 一維指標指向二維陣列

看板C_and_CPP作者 (5566 超強)時間12年前 (2012/01/09 18:38), 編輯推噓0(006)
留言6則, 5人參與, 最新討論串1/1
請問一下我想要用一維指標指向二維陣列 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

01/09 18:43, , 1F
ptr = &array2D[0][0];
01/09 18:43, 1F

01/09 20:44, , 2F
ptr = (int*)array2D;
01/09 20:44, 2F

01/09 20:45, , 3F
ptr = array2D[0]
01/09 20:45, 3F

01/09 20:52, , 4F
int (*ptr)[8];
01/09 20:52, 4F

01/09 20:56, , 5F
不過這樣就要寫成*(*(pt + i) + j) 嗯……
01/09 20:56, 5F

01/10 11:44, , 6F
感謝大大問題解了~
01/10 11:44, 6F
文章代碼(AID): #1F2iEhtL (C_and_CPP)