【问题标题】:(C Programming) How do I use a data structure and function which are defined in another file?(C 编程)如何使用在另一个文件中定义的数据结构和函数?
【发布时间】:2019-07-18 18:45:46
【问题描述】:

说,我有一个文件 dataStructure.cdataStructure.h。 (我的数据结构是一个哈希映射。)这些文件包含数据结构的实现以及向结构添加新条目以及检索条目的方法。

这是一个例子:

// dataStructure.h

struct node {
    char *label;
    int address;
    struct node *next; // Points to the next node.
};

struct ourTable {
    int size;
    struct node **list; // List of all the (key, value) pairs.
};

// Then, here are methods to create table, add new entries and retrieving them.
struct ourTable createTable(int size);

void addEntry(struct ourTable *t, char *label, int address);

unsigned retrieveAddress(struct ourTable* table, char *label);

函数retrieveAddress 基本上只是返回该标签的地址。因为我正在尝试实现一个哈希映射,它只是几个(键,值)对的数据结构。在我的例子中,键是label,而值是address

unsigned retrieveAddress( struct ourTable* table, char *label)
{
    int bin = 0;
    bin = hashFunction(table, label); // Hashing function

    struct node *list = table->list[bin];
    struct node *entryItem = list;
    while(entryItem)
    {
        if (entryItem->label == label)
        {
            return entryItem->address; // Returns the address of that label.
        }
        entryItem = entryItem->next;
    }
    return NULL;
}

然后,我有另一个文件establishTable.c,它只是使用dataStructure.h 中实现的方法来创建一个表,然后添加新条目。这是我在那个文件中写的:

// establishTable.c

#include "dataStructure.h"

struct ourTable establishTable()
{    
    struct ourTable table = createTable(1000); // Create a table with a maximum of 1000 entries.
    addEntry(&table, "word", 1234);
}

我想要做的是将结构ourTable 与我在establishTable.c 中插入的新条目传递到主文件main.c。为了说明我想要完成的工作:

// main.c

#include "dataStructure.h"

#include "establishTable.h"

int main()
{
    // I'm not sure how to pass the data structure... Something like this:
    struct ourTable table = establishTable(); 

    // Get the retrieveLabel function from dataStructure.h and use it here.
    printf("retrieved from the table at %u\n\n", retrieveAddress(&table,"word") );
}

我尝试运行main.c。它没有显示任何错误,但它只是输出

retrieved from the table at 0

这只是告诉我我已经建立的表根本没有传递。输出应该是 1234。

那么,如何将数据结构和函数结果从另一个文件传递到我的main.c 文件?当我在establishTable.c 中做所有事情时它就可以工作,但这不是我的意图。我已经按照其他线程中的建议尝试了 extern 方法,但没有任何效果。

【问题讨论】:

  • 您应该启用所有编译器警告。并听他们的。编译器应该抱怨 establishTable 没有返回任何东西。
  • 在不知道retrieveLabel 应该返回什么的情况下,我们无法判断是否有问题。由于“word”是唯一的条目,位置 0 似乎并不那么糟糕。你期望的输出是什么?
  • 请提供MCVE
  • 什么是hashFunctionaddEntry 是什么? retrieveAddress 返回一个 unsigned int,但它应该返回一个 struct node *establishTable 不返回任何内容。提示:编译时启用所有警告并假装警告是错误。
  • 您认为您的问题是这些函数位于单独的文件中。我可以保证不是:如果您将所有内容都放入main.c(是的,这是可能的),它仍然无法正常工作。如果它碰巧起作用了,那仅仅意味着那次你很幸运 - 错误仍然存​​在。

标签: c gcc data-structures hashmap include


【解决方案1】:

你忘记了return语句

struct ourTable establishTable()
{    
    struct ourTable table = createTable(1000); // Create a table with a maximum of 1000 entries.
    addEntry(&table, "word", 1234);
    return table;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    相关资源
    最近更新 更多