簡體   English   中英

從傳遞給函數的嵌套結構訪問信息

[英]Accessing information from nested struct passed to function

我有以下代碼:

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中的struct wordPair中訪問數據? 我需要該函數返回englishWordforeignWord的strdup。

我正在嘗試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"},
};

這是我在嘗試的第一個測試中調用函數的方式,這是當translation函數傳遞英語單詞並要求返回外來單詞時:

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)以外的值。 我是調試器的新手,所以我確信,如果我知道如何閱讀它,它可以告訴我更多信息。

尚不完全清楚您的函數將要執行的操作,但是可能合法工作的最簡單的實現是:

#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元素本身不需要連續分配。 假定struct wordPair的連續數組不成問題,以下代碼是更傳統的定義:

#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;
}

給定示例測試代碼,其中dictionary_translate()的參數之一是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;
}
d->(*data)->englishWord

應該編譯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM