[問題] 二維陣列傳遞問題
看板C_and_CPP (C/C++)作者uncleuse (Uncleuse)時間16年前 (2009/02/16 22:06)推噓8(8推 0噓 30→)留言38則, 5人參與討論串1/2 (看更多)
請問大家,我寫一個程式如下:
#include<iostream>
using namespace std;
void PrintArray(int* a);
int main( )
{
int Num[2][5] = { 1,2,3,4,5,
6,7,8,9,10};
PrintArray(Num+5);
// 這裡編譯有誤:
// cannot convert `int (*)[5]' to `int*'
// for argument `1' to `void PrintArray(int*)'
getchar( );
return 0;
}
void PrintArray(int* a)
{
for(int i=0;i<5;i++)
cout << a[i] <<" " ;
}
但是,在我宣告Num[2][5]時,記憶體應該會是線性儲存變數:
1,2,3,4,5,6,7,8,9,10
我是將陣列名Num當成是指向第一個數字的指標,
所以我將Num+5之後應該就是指向6這個數字的指標,
然後再傳入PrintArray(int* a)試著將它印出.
但是編譯有誤, 請問我哪個觀念想錯?
感謝大家~~~~~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 210.64.114.55
推
02/16 22:19, , 1F
02/16 22:19, 1F
推
02/16 22:21, , 2F
02/16 22:21, 2F
→
02/16 22:23, , 3F
02/16 22:23, 3F
→
02/16 22:23, , 4F
02/16 22:23, 4F
→
02/17 10:02, , 5F
02/17 10:02, 5F
推
02/17 12:30, , 6F
02/17 12:30, 6F
→
02/17 12:30, , 7F
02/17 12:30, 7F
→
02/17 12:31, , 8F
02/17 12:31, 8F
→
02/17 12:33, , 9F
02/17 12:33, 9F
→
02/17 17:22, , 10F
02/17 17:22, 10F
→
02/17 17:25, , 11F
02/17 17:25, 11F
→
02/17 17:26, , 12F
02/17 17:26, 12F
→
02/17 17:28, , 13F
02/17 17:28, 13F
推
02/17 17:56, , 14F
02/17 17:56, 14F
→
02/17 17:58, , 15F
02/17 17:58, 15F
推
02/17 18:05, , 16F
02/17 18:05, 16F
→
02/17 18:06, , 17F
02/17 18:06, 17F
→
02/17 18:13, , 18F
02/17 18:13, 18F
推
02/17 19:49, , 19F
02/17 19:49, 19F
→
02/17 19:49, , 20F
02/17 19:49, 20F
→
02/17 19:49, , 21F
02/17 19:49, 21F
→
02/17 19:49, , 22F
02/17 19:49, 22F
→
02/17 19:50, , 23F
02/17 19:50, 23F
→
02/17 19:50, , 24F
02/17 19:50, 24F
→
02/17 19:50, , 25F
02/17 19:50, 25F
→
02/17 19:50, , 26F
02/17 19:50, 26F
→
02/17 19:50, , 27F
02/17 19:50, 27F
→
02/17 19:51, , 28F
02/17 19:51, 28F
→
02/17 21:21, , 29F
02/17 21:21, 29F
→
02/17 21:24, , 30F
02/17 21:24, 30F
→
02/17 21:26, , 31F
02/17 21:26, 31F
→
02/17 21:27, , 32F
02/17 21:27, 32F
→
02/17 21:29, , 33F
02/17 21:29, 33F
→
02/17 21:30, , 34F
02/17 21:30, 34F
→
02/17 21:30, , 35F
02/17 21:30, 35F
→
02/17 21:31, , 36F
02/17 21:31, 36F
推
02/17 21:33, , 37F
02/17 21:33, 37F
推
02/18 02:19, , 38F
02/18 02:19, 38F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章