2014-04-13 6 views
0

를 함수에 전달 나는 다음과 같은 코드가 있습니다액세스 정보는

struct wordPair { 
     char* englishWord; 
     char* foreignWord; 
}; 

struct dictionary { 
     struct wordPair ** data; 
     int nbwords; 
     int size; 
}; 

내가 어떤 데이터로 채워 struct dictionary *dictionaryPtr을 말해봐, 내가 다음 함수에 전달 :

char* dictionary_translate(struct dictionary* d, 
         const char* const english_word, 
         const char* const foreign_word) 

내를 함수 dictionary_translate, 전달 된 구조체 내에 중첩 된 struct wordPair의 데이터에 어떻게 액세스 할 수 있습니까? englishWord 또는 foreignWord 중 하나를 반환하는 함수가 필요합니다.

나는 d->data->englishWord을 시도했지만 "구조체 또는 공용체가 아닌 것"에 englishWord '멤버 요청 "오류가 표시됩니다.

업데이트!

내가 수행해야 할 함수 dictionary_translate은 전달 된 단어 중 하나를 포함하는 일치하는 단어 쌍이 있는지 판단하고 번역본의 strdup (쌍의 다른 단어)을 반환합니다.

const char* test_translations[NB_TESTS][NB_COLS] = 
{ 
    {"hello", "hola"}, 
    {"cat", "gato"}, 
    {"dog", "perro"}, 
    {"thanks", "gracias"}, 
    {"pants", "pantalones"}, 
    {"shoes", "zapatos"}, 
}; 

이 나는 ​​기능은 영어 단어를 전달 번역 할 때입니다 필요합니다 내가 노력하고있어 첫 번째 테스트에서 함수를 호출하고있어 어떻게 : 여기가 정의한 단어의 배열입니다 외래어를 반환 :

여기
char* translationPtr = NULL; 

for (i = 0; i < NB_TESTS; i++) { 
    translationPtr = dictionary_translate(dictionaryPtr, test_translations[i][0], NULL); 
    printf("English Word %s translated: %s\n", test_translations[i][0], translationPtr); 
} 

내가 지금까지이 같은 기능을 번역입니다 ...

char* dictionary_translate(struct dictionary* d, 
          const char* const english_word, 
          const char* const foreign_word){ 
    int i; 

    if (d == NULL) return NULL; 

    for (i = 0; i < d->nbwords; i++) { 
     if (strcmp(english_word, d->data[i]->englishWord) == 0) 
      return strdup(d->data[i]->foreignWord); 
     else if (strcmp(foreign_word, d->data[i]->foreignWord) == 0) 
      return strdup(d->data[i]->englishWord); 
    } 

    return NULL; 
} 

을 즉시 프로그램이 변환 기능에 도달, 그것은 충돌합니다. 무슨 일이 일어나고 있는지 알기 위해 디버거를 이해할 수는 없지만 translationPtr에는 NULL (0x0)이 아닌 다른 값이없는 것 같습니다. 나는 디버거에 익숙하지 않아서 읽는 방법을 안다면 더 많이 알려줄 것이라고 확신한다.

+0

데이터가 배열에 대한 포인터 인 것처럼 보이기 때문에 d -> (* data) -> englishWord와 같이 데이터를 참조 해제해야 할 수도 있습니다. – HypnoToad

+0

@DoctorZero : 구문 상 유효하지 않습니다. –

답변

-3
d->(*data)->englishWord 

컴파일해야합니다.

+1

표현의 편집 가능성에 대한 견해를 보려면 컴파일러와상의해야한다고 생각합니다. 내 견해 (그리고 나는 C 컴파일러가 아님을 인정한다)은 구문 상 올바르지 않다는 것이다. –

+0

맞습니다. 시도 해봐. –

+0

나는 예의를 갖춰야했습니다. C 컴파일러를 지나서 코드를 실행해야합니다. 컴파일되지 않습니다. 컴파일 할 C 컴파일러가있는 경우 전체 세부 정보를 제공하십시오. 'if (strcmp (d -> (* data) -> englishWord, "aleph") == 0) 라인을 추가하면 NULL을 반환하고, Mac OS X 10.9.2 매버릭스에서 GCC 4.8.2로 연결되었습니다. 'dict.c : 함수에서'dictionary_translate ':와 dict.c : 24 : 19 : 오류 : 예상 식별자가'('token'일 때 at if (strcmp (d -> (* data) - > englishWord, "aleph") == 0) 캐럿'^'은'(* 데이터)'의'(')을 의미한다. –

0

당신의 기능이해야 할 무엇인지 완전히 명확하지 않다, 그러나 합법적으로 일할 수있는 간단한 구현에 관한 것입니다 :

#include <string.h> 

struct wordPair 
{ 
    char *englishWord; 
    char *foreignWord; 
}; 

struct dictionary 
{ 
    struct wordPair **data; 
    int nbwords; 
    int size; 
}; 

extern char *dictionary_translate(struct dictionary *d, 
            const char *const english_word, 
            const char *const foreign_word); 

char *dictionary_translate(struct dictionary *d, 
          const char *const english_word, 
          const char *const foreign_word) 
{ 
    for (int i = 0; i < d->nbwords; i++) 
    { 
     if (strcmp(english_word, d->data[i]->englishWord) == 0) 
      return strdup(d->data[i]->foreignWord); 
     else if (strcmp(foreign_word, d->data[i]->foreignWord) == 0) 
      return strdup(d->data[i]->englishWord); 
    } 
    return 0; 
} 

나는 당신이 당신의 struct dictionary의 디자인을 검토한다고 생각합니다. 이중 포인터를 사용하는 것은 불필요한 것으로 보입니다 (또는 사용 이유가 분명하지 않음). 유일한 이점은 struct wordPair에 대한 포인터의 연속적인 배열을 가지며 실제 struct wordPair 요소는 연속적으로 할당 될 필요가 없다는 것입니다. 다음 코드는 struct wordPair의 연속 배열이 문제가되지 않습니다 가정, 더 정통 정의입니다 : 코드, dictionary_translate()에 인수 중 하나가 NULL 포인터 인 샘플 테스트 코드를 감안할 때

#include <string.h> 

struct wordPair 
{ 
    char *englishWord; 
    char *foreignWord; 
}; 

struct dictionary 
{ 
    struct wordPair *data; 
    int nbwords; 
    int size; 
}; 

extern char *dictionary_translate(struct dictionary *d, 
            const char *const english_word, 
            const char *const foreign_word); 

char *dictionary_translate(struct dictionary *d, 
          const char *const english_word, 
          const char *const foreign_word) 
{ 
    for (int i = 0; i < d->nbwords; i++) 
    { 
     if (strcmp(english_word, d->data[i].englishWord) == 0) 
      return strdup(d->data[i].foreignWord); 
     else if (strcmp(foreign_word, d->data[i].foreignWord) == 0) 
      return strdup(d->data[i].englishWord); 
    } 
    return 0; 
} 

이 함수가 null 인 경우 인수를 역 참조하지 않도록 수정해야합니다. 이중 포인터 버전은 struct dictionary으로 가정합니다.

char *dictionary_translate(struct dictionary *d, 
          const char *const english_word, 
          const char *const foreign_word) 
{ 
    for (int i = 0; i < d->nbwords; i++) 
    { 
     if (englishWord != NULL && strcmp(english_word, d->data[i]->englishWord) == 0) 
      return strdup(d->data[i]->foreignWord); 
     else if (foreignWord != NULL && strcmp(foreign_word, d->data[i]->foreignWord) == 0) 
      return strdup(d->data[i]->englishWord); 
    } 
    return 0; 
} 
+0

입력 해 주셔서 감사합니다. 원래 질문을 업데이트했습니다. 제안 된 기능을 구현했습니다 (강사가 추가해야하는 몇 가지 추가 기능 포함). 더 많은 정보. 문제는 현재 실행 중일 때 충돌하지만 중첩 된 구조체 내의 정보에 액세스하는 방법은 훌륭하게 작동하는 것처럼 보입니다! – xfriendsonfirex