[問題] C語言 struct 傳值問題

看板C_and_CPP作者 (木村拓拉)時間13年前 (2010/10/21 22:44), 編輯推噓3(308)
留言11則, 6人參與, 最新討論串1/1
我想請問大家 我宣告了一個struct如下: struct ARRAY { unsigned int index; unsigned int level; unsigned int pos; unsigned short valid; unsigned int ip[8]; unsigned int len; }; 之後 struct ARRAY *TCAM; TCAM = (struct ARRAY *)malloc(entry_table_size*sizeof(struct ARRAY)); 之後我將TCAM裡面的值存滿 但當我之後的function 打算依序將TCAM內的ip值依序傳遞作大小的比較 如下 0 void function(){ 1 2 if(compare(key, &TCAM[j]) == 1){ 3 ..................; 4 } 5 } int compare(struct CAM *key, struct CAM *item); 但我在compare中將ip[0]~ip[7]依序printf出來 卻是錯誤的值 但我實在不知道哪裡出錯了 我在上面finction的程式第2行傳入&TCAM[j]之前 有先printf過 值是正確的 而compare這個函式在之前我也有使用過是無誤的 所以我想是不是我傳值的方式有錯誤呢? 請教一下板上的各位先進們! 先謝過 補充一下: 我在compare中printf的方式是 for(k=0 ; k<8 ; k++) printf("%d ", item->ip[k]); -- → repairhate77:可能檢起來會加10兵力吧? 08/25 23:53 → repairhate77: 撿 08/25 23:53 → repairhate77: 撿 08/25 23:53 → repairhate77: 撿 08/25 23:54 → repairhate77:可能撿 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.255.74.58 ※ 編輯: LBJALA 來自: 111.255.74.58 (10/21 22:46)

10/21 22:48, , 1F
&TCAM[j]?????????????????????????????????
10/21 22:48, 1F
請問這樣傳值的方法錯了嗎?

10/21 22:50, , 2F
把程式碼都貼出來吧,這樣看少一堆資訊,用猜的很累
10/21 22:50, 2F
我可以貼出來 可是蠻長的 我怕大家看了更複雜 因為我的程式寫的不是很好看 ///////////////////////////////////////////////////////// int compare(struct CAM *key, struct CAM *item) { char key_bin[128], item_bin[128]; int j, l, b, k; printf("compare:\n"); printf("item:"); for(k=0 ; k<8 ; k++) printf("%x ", item->ip[k]); printf("/%d\n", item->len); printf("key:"); for(k=0 ; k<8 ; k++) printf("%x ", key->ip[k]); printf("/%d\n", key->len); } ////////////////////////////////////////////////////////// void deletion(struct CAM *key, int start_level) { int i, j, k; int child_hit_flag = 0; int parent_hit_flag = 0; int sibling_hit_flg = 0; int TCAM_index = 0; int current_level, next_level, child_pos, parent_pos; struct ARRAY *ptr; printf("開始刪除\n"); for(i=start_level ; i<=G_level ; i++) { for(j=level_start_ptr[i]->index ; j<=level_finish_ptr[i]->index ; j++){ printf("j:%d\n", j); for(k=0 ; k<8 ; k++) printf("%x ", TCAM[j].ip[k]); printf("/%d\n", TCAM[j].len); for(k=0 ; k<8 ; k++) printf("%x ", key->ip[k]); printf("/%d\n", key->len); getchar(); /*這裡*/ if(compare(key, &TCAM[j]) == 0) ^^^^^^^^ { child_hit_flag = 1; current_level = i; next_level = i+1; child_pos = j; break; } } } } ////////////////////////////////////////////////////// 我想問的問題就是在我標記的那一行 我想將值傳到compare中作處理 但值傳過去都是錯的 麻煩大家幫幫我吧 ※ 編輯: LBJALA 來自: 111.255.74.58 (10/21 23:00)

10/21 22:57, , 3F
我覺得&TCAM[j]已經不知道指向何方了
10/21 22:57, 3F

10/21 22:59, , 4F
compare 要接兩個 struct CAM* 你給他 struct ARRAY*
10/21 22:59, 4F

10/21 22:59, , 5F
把&TCAM[j]改成TCAM就好
10/21 22:59, 5F

10/21 23:00, , 6F
型態都不對了, 你能期望他作對什麼事?
10/21 23:00, 6F

10/21 23:02, , 7F
感謝loveme00835大可能說到重點了我沒注意到 也感謝其他人
10/21 23:02, 7F

10/21 23:04, , 8F
我以為那是KEY錯XD
10/21 23:04, 8F

10/22 02:10, , 9F
&TCAM[j] 不需要&,這樣只是比對指標本身
10/22 02:10, 9F

10/22 02:14, , 10F
XD 樓上那 qsort 的第四個參數怎辦?
10/22 02:14, 10F

10/22 02:17, , 11F
TCAM[j]
10/22 02:17, 11F
文章代碼(AID): #1Cm56-od (C_and_CPP)