2010-11-23 31 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格式说明符。