2010-11-23 4 views
1

구조체에 값을 할당 한 후 값을 읽으려고 할 때 이상한 프로그램 동작이 나타납니다. 내가 문제를 강조하기 위해 내 기능의 대부분을 주석 한할당 후 C에서 struture 값을 읽지 못했습니다.

/*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

무엇을 기대 했습니까? 추신. 당신은 여분의 코드를 제거하고 싶을 수도 있습니다 –

+0

나는 여기에있는 문제를 이해하지 못합니다 ... 출력에 따르면, 과제는 정확합니다! 'cor_table_data'는 0이 아니기 때문에 문제입니까? – filipe

+1

@Martin @filipe 코드 읽기가 마음에 들지 않으면 의견을 남기지 마십시오. 문제는 분명하고 명확합니다. –

답변

4

-Wall으로 컴파일하십시오. GCC는 % 형식 지정자와 printf() 인수의 크기가 일치하지 않는다고 알려야합니다. %x 대신 %llx을 시도하십시오. 그게 문제를 해결해야합니다.

+1

'entry_data'가'long long' 때문에'% llx'이되었습니다. –

+0

안녕하세요, 저는 이것을 시도했지만, 두 개의 인쇄 된 값 외에는 여전히 긴 타입의 unsigned int를 가지고 있습니다.), 다른 방식으로 표시해서는 안됩니다. – cwei

+0

'cor_table_data'를 잘 출력 해주세요. –

0

아마도 printf를 사용하는 것이 문제 일 수 있습니다. % x은 (는) 서명되지 않은 long long을 표시하도록 설계되지 않았습니다. 인쇄하기 전에 값을 분할하면 예상 한 값이됩니다.

컴파일러에서 지원하는 경우 %llx 형식 지정자를 사용할 수도 있습니다.

+0

내 대답에 그 사람을 기쁘게하지 않는 것을 downvoter가 설명합니까? 형식 지정자를 알고 있지만 인쇄하기 전에 값을 분할해도 작동합니다! 그리고 upvoted 대답은 (아직) 잘못된 형식 지정자를 사용하기 때문에 (아직) ... – kriss

+0

나는 일을하는 데 (단 하나) 길 밖에 없다고 믿는 사람들에게 화를 내기 시작했습니다. 그것은 프로그래밍이 아니라 종교 (그리고 그것의 나쁜 측면)입니다. – kriss

+0

유연성 부족은 종교 만의 영역이 아닙니다. :) – ongle

관련 문제