Re: [問題] *ptrptr++ 與 (*ptrptr)++

看板C_and_CPP (C/C++)作者 (我愛阿蓉)時間16年前 (2009/07/24 22:02), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/3 (看更多)
※ 引述《IDontBite (大便兔子)》之銘言: : void testing(int **ptr){ : cout<<*ptr<<endl; : *ptr++; 這行 其實是 ptr++; *ptr; 所以原本 ptr可能= int *p的&p 你指到不知到哪了 和(*ptr)++不一樣 後面就是你所說的 我剛剛跑了一下 如果這樣寫 我用vc跑 他return 0結束main後 竟然會出現error Run-Time Check Failure #2 - Stack around the variable 'p' was corrupted. 這就請各位 解釋吧! : cout<<*ptr<<endl; : (*ptr)++; : cout<<*ptr<<endl; : *ptr = *ptr + sizeof(int); : cout<<*ptr<<endl; : return;} : 以上這段程式碼, 傳一個int*的位址進去之後得到以下結果 : 0x19 : 0x3e3970 : 0x3e3974 : 0x3e3984 : 從2到3行應該是*ptr前進一個int大小, 所以+4 : 3到4行是*ptr前進sizeof(int) = 4個int大小, 所以+16, 用16進制所以看起來像+10 : 但第1行到第2行到底發生了甚麼事 <囧> 這個bug搞了我一整天, 很想弄清楚 : 麻煩各位了<(_ _)> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.207.187
文章代碼(AID): #1AQRxnMs (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1AQRxnMs (C_and_CPP)