作者Scofield ( 2pac )
站內Programming
標題Re: [問題] C++以函數傳送陣列,而以指標方式接收
時間Sun Apr 15 21:25:49 2007
※ 引述《Mewra ()》之銘言:
: ※ 引述《LPH66 (ha(ruhi|yate)ism)》之銘言:
: : 因為你的迴圈變數ptr雖然是從0~size-1
: : 但中間卻去拿了ptr和ptr+1兩個位置來比
: : 於是當ptr=size-1時就抓到不該抓的資料了
: 想請問一下一個類似的問題, 若參數只傳遞陣列不傳遞陣列size
: void waha( string tester[] );
: int main()
: {
: string test[5] = { "ab", "bc", "cd", "de", "ef" };
: waha( test );
: }
: void waha( string tester[] )
: {
: string *tester2;
: tester2 = &tester[0];
: // 這裡怎麼算出tester裡有幾個element?
: }
: 在不傳遞test的size進waha的情況下,
: 在waha裡, 有辦法算出tester這個array的大小嗎?
: 謝謝
宣告這個
int count = (sizeof tester)/(sizeof tester[0]);
即可
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.18.140
推 avhacker:別傻了. tester 當時已變成 pointer 了 140.123.19.235 04/15 21:57
推 Scofield:對不起喔...我忽略了 140.112.18.140 04/15 22:26