c - 从传递给函数的嵌套结构访问信息

标签 c struct nested

我有以下代码:

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

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

假设我有 structdictionary*dictionaryPtr 填充了一些数据,并将其传递给以下函数:

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

在函数 dictionary_translate 中,如何访问嵌套在传递的结构中的 struct wordPair 中的数据?我需要该函数返回 englishWordforeignWord 的 strdup。

我正在尝试d->data->englishWord,但这给了我错误“请求非结构或 union 中的成员'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) 以外的值。我是调试器的新手,所以我确信如果我知道如何阅读它,它可以告诉我更多信息。

最佳答案

尚不完全清楚您的函数要做什么,但可能合法工作的最简单实现是:

#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 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 指针,因此必须修改函数中的代码,以便在参数为 null 时不要取消引用该参数。这假定是结构字典的双指针版本。

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

关于c - 从传递给函数的嵌套结构访问信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23038869/

相关文章:

c - 函数根本不返回值 - 不是 void

python - SciPy 的 ctypes Fibonacci 示例无法运行,并出现 "array must have data type int64"错误

c++ - 共享内存信号量

包含结构的 C union - 内存映射 - 编译器跳过一个字节?

c# - 在c#中读取封装在对象中的结构体数组

json - 忽略 struct 中的对象为 nil,而不是当它是空数组时

Javascript 遍历对象并在子对象中插入丢失的键

c - vugen 出现 "Message id [-13992] was not saved - Auto log cache is too small to contain the message"错误

javascript - 解析包含括号的 jQuery 元素的函数

c++ - (c++) 可以从外部来源嵌套命名空间