2012-01-10 8 views
0

호출 할 때 구조체가 제대로 작동하지 typedef에 배열하고 정확하게 데이터에서하여 ARAY에게내가</p> <pre><code>typedef struct LABELS_STRUCT { char *name; int address; } `LABELS_STRUCT;` LABELS_STRUCT labels_array[ARRAY_LABELS]; </code></pre> <p>를 선언 기능

int add_data_label(char * label, int displace) 
{ 
    LABELS_STRUCT x = {label,DATA_STARTING_ADDRESS + (100 * displace)}; 
    labels_array[initialize]=x; 
    initialize++;//=initialize+displace; 
    printf("\tAdding [%s] with offset [%d] to labels_array[%d] with address [%d]\n", label,displace,initialize,CODE_STARTING_ADDRESS + (100 * displace)); 
    printf("\tlabels_array[%d].name=[%s] and labels_array[%d].address= [%d]\n", initialize, labels_array[initialize].name, initialize, labels_array[initialize].address); 

    return 1; 
} 

두 번째 printf() 문 인쇄 주소 이름을 추가하고 기능 그러나 다른 기능이있을 때

int get_label_address(char *label) { 
    int i; 
    printf("Getting the Label Address for [%s]\n", label); 
    for (i = 0; i < initialize; i++) { 
     printf("\t\t\tCOMPARING LABEL [%s] TO [%s] at index [%d]\n", label, labels_array[i].name,i); 
     if (labels_array[i].name==label) { 
      printf("Label_Array[%d]=[%s] Matches label=[%s] with address [%d]\n",i,labels_array[i].name,label,labels_array[i].address); 
      return labels_array[i].address; 
     } 
    } 
    return 0; 
} 

디버그 printf() statement "COMPARING LABEL [%s] to [s]..." 루프는 null과 비교됩니다. labels_array[i].name에서 다른 함수를 사용하여 요소를 인쇄하면 name 요소에는 공백 값이 표시되지만 address 요소에는 공백 값이 표시됩니다. 배열에 넣은 세 개의 요소가 있다고 가정하면 labey_array[1 2 and 3]은 빈 줄이되지만 label_array[4 and on]null이됩니다. 그래서 그것은 그것이 초기화되었다는 것을 알지만 실제 이름은 보이지 않고 단지 빈 줄만 보여줍니다.

누구든지 아이디어가 있습니까?

답변

1

기능 add_data_label()에서 당신은 LABELS_STRUCTchar *name;에 들어오는 char * labelstrcpy()을 수행해야합니다. 또한 에 대한 메모리는 LABELS_STRUCTxmalloc()을 사용하여 할당해야합니다.

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

typedef struct LABELS_STRUCT { 
    char *name; 
    int address; 
} LABELS_STRUCT; 

#define ARRAY_LABELS 10 

LABELS_STRUCT labels_array[ARRAY_LABELS]; 
int labels_array_index; 

int add_data_label(char * label, int displace) 
{ 
    int len = strlen(label); 
    printf("len = %d \n", len); 

    if ((labels_array[labels_array_index].name = (char *) malloc(sizeof(char) * len + 1)) == NULL) { 
     printf("unable to allocate memory \n"); 
     return -1; 
    } 
    strcpy(labels_array[labels_array_index].name, label); 
    printf("name = %s \n", labels_array[labels_array_index]); 

    labels_array[labels_array_index].address = displace; 
    labels_array_index++; 
    /* here you can copy displace to LABELS_STRUCT's address */ 
} 

int main(void) 
{ 
    labels_array_index = 0; 
    add_data_label("abc", 19); 
    return 0; 
} 

몇 가지 포인트 참고하기 :

기본적으로, 다음과 같은 뭔가가 필요

  • 당신이해야 free() 모든 malloc() '에드 메모리. 따라서 add_data_label()과 마찬가지로 delete_data_label()이 있고 namefree()으로 전화해야합니다. 원래 코드에서
  • , 내가 initialize를 추측하는 것은 initialize++; 단지 디버그 인쇄 문을 수정 얻을 반환하기 전에 작성해야 기능 add_data_label() 문에서 유효한 값
  • 초기화됩니다.
+0

당신은 생명의 구원자입니다. 나는 12 시간 이상을 보내고 자 노력했다. –

+0

@AironZagarella 나는 기뻐요! :) 행운을 빕니다!! –

+0

@RaviSharma 좋은 지적! 메모를 추가해 주셔서 감사합니다. :) –

관련 문제