2010-11-23 26 views
1

我在嘗試從結構中爲值分配值之後讀取值時出現了奇怪的程序行爲。我顯示下面的相關結構和功能:在分配值後,無法從C中的結構讀取值

/*Data struct for cor_entry */ 
struct cor_entry { 
    struct cor_entry * pre_entry; 
    struct cor_entry * next_entry; 
    long long unsigned int entry_data; 
}; 

我註釋掉我的大部分功能的突出問題:

/* update correlation table */ 
void cor_table_update(long long unsigned int cor_table_data, 
    struct cor_entry **cor_table_head_ptr, 
    struct cor_entry **cor_table_tail_ptr, 
    int *entry_num, 
    const int MAX_NUM) 
{ 
    struct cor_entry *cor_table_entry; 
    int cor_hit=0; 

    //test code 
    //cor_table_head=cor_table_tail=(struct cor_entry*)calloc(1, sizeof(struct cor_entry)); 
    //printf("original cor_entry_num=%d\n",*entry_num); 

    ////////////////////////code for test/////////////////////////////// 

    cor_table_entry=(struct cor_entry*)calloc(1, sizeof(struct cor_entry)); 
    printf("The cor_table_entry=%x\n",cor_table_entry); 
    cor_table_entry->entry_data=cor_table_data; 
    if (cor_table_entry->entry_data==cor_table_data) 
    { 
     printf("The assignment is correct!\n"); 
     printf("the cor_enrty_data=%x, stored data=%x,\n", 
      cor_table_data, 
      cor_table_entry->entry_data); 
    } 

    // ... rest of function 
} 

而且我得到這個輸出運行程序時:

 
The cor_table_entry=8c09a58 
The assignment is correct! 
the cor_enrty_data=8ffc8, stored data=0, 
The cor_table_entry=8c09a70 
The assignment is correct! 
the cor_enrty_data=8ffc8, stored data=0, 
The cor_table_entry=8c09a88 
The assignment is correct! 
the cor_enrty_data=8ffc8, stored data=0, 
The cor_table_entry=8c09ae8 

有人可以解釋一下這個問題嗎?我正在使用GCC-3.4.6編譯器。

+2

你期待什麼? PS。你可能想刪除多餘的代碼 – 2010-11-23 16:33:07

+0

我不明白這裏有什麼問題......根據你的輸出,分配是正確的! `cor_table_data`不應該是0,這是問題嗎? – filipe 2010-11-23 16:36:33

+1

@Martin @filipe如果您不想閱讀代碼,請不要發表評論。問題是顯而易見的。 – 2010-11-23 16:38:20

回答

4

嘗試使用-Wall進行編譯。 GCC應該告訴你%格式說明符和printf()參數的大小不匹配。嘗試%llx而不是%x。這應該解決這個問題。

0

你的問題可能與printf有關,%x不是用來顯示long long unsigned。在打印之前將值拆分,這應該是您所期望的。

如果您的編譯器支持,您還可以使用%llx格式說明符。