作者csihcs (非天夜翔)
看板C_and_CPP
標題Re: [問題] function call by reference的問題
時間Fri Feb 17 01:20:30 2012
※ 引述《lueichun (no anonymous)》之銘言:
: 開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
: dev-c++
: 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
: 問題(Question):
: 在code內,有個叫make_tree的函數,在這函數執行時會有bug
: 餵入的資料(Input):
: 預期的正確結果(Expected Output):
: 原本預期run完這函數就能將tree建起來
: 錯誤結果(Wrong Output):
: 結果head吃到elememt的位置之後,好像發生將位置丟掉的狀況,每次都去執行一開始
: if(...)的部分
: 程式碼(Code):(請善用置底文網頁, 記得排版)
: http://ideone.com/gaV2M
: 補充說明(Supplement):
: 我把head當作是tree的開頭位置,然後根據head是否有內容決定student_ptr的位置,
: 不知道這樣是錯在哪了???
個人認為這個函數改點部分內容,如下:
此外這個函數明明就不是 call by reference @.@"
void make_tree(struct STUDENT* student_ptr,struct STUDENT* head)
void make_tree(struct STUDENT* student_ptr,struct STUDENT* parent)
{
if(head==NULL)
{
head=student_ptr;
printf("%d\n",head->id);
/*current=student_ptr;
previous=student_ptr;*/
}
else if(head!=NULL)
{
// 此行以下 head,置換成 parent
if(head->id<=student_ptr->id)
if(parent->id<=student_ptr->id)
{
/*previous=current;
current=head->left_next;*/
if(head->left_next==NULL)
{
head->left_next=student_ptr;
}
else if(head->left_next!=NULL)
{
make_tree(student_ptr,head->left_next);
}
}
else if(head->id>student_ptr->id)
{
if(head->right_next==NULL)
{
head->right_next=student_ptr;
}
else if(head->right_next!=NULL)
{
make_tree(student_ptr,head->right_next);
}
}
}
//printf("%s %d\n",head->name,head->id);
}
--
渴望飛翔在
自由的
風中,
期望逃離這拘束的
現實,
一切都讓他隨著
風而去,
獨自躲在
黑暗的
空氣中,
舔舐被狠狠撕裂的
傷口。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.167.7.192
→ lueichun:可以說明一下原因嗎,謝謝 02/17 02:17
→ diabloevagto:上面那個是pointer你可以在找一下ref是要怎樣寫 02/17 09:43
→ csihcs:原因我不是在上一篇推文說了嗎@@" 02/17 23:11
→ lueichun:現在懂了,原來我全域區域不分~"~ 02/18 17:44