首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未声明的“Table1”(在此函数中首次使用)

未声明的“Table1”(在此函数中首次使用)
EN

Stack Overflow用户
提问于 2017-05-09 15:37:29
回答 2查看 91关注 0票数 1

在编译此代码时,我收到以下错误。

代码语言:javascript
复制
tables/cuckoo.c: In function 'new_cuckoo_hash_table':
tables/cuckoo.c:35:9: error: invalid type argument of '->' (have 'CuckooHashTable')
  table1 -> slots = malloc((sizeof *table1->slots) * size);
         ^
tables/cuckoo.c:35:42: error: invalid type argument of '->' (have 'CuckooHashTable')
  table1 -> slots = malloc((sizeof *table1->slots) * size);
                                          ^
In file included from tables/cuckoo.c:11:0:
tables/cuckoo.c:36:15: error: invalid type argument of '->' (have 'CuckooHashTable')
  assert(table1->slots);
               ^
tables/cuckoo.c:37:8: error: invalid type argument of '->' (have 'CuckooHashTable')
  table1->inuse = malloc((sizeof *table1->inuse) * size);
        ^
tables/cuckoo.c:37:40: error: invalid type argument of '->' (have 'CuckooHashTable')
  table1->inuse = malloc((sizeof *table1->inuse) * size);
                                        ^
In file included from tables/cuckoo.c:11:0:
tables/cuckoo.c:38:18: error: invalid type argument of '->' (have 'CuckooHashTable')
     assert(table1->inuse);

我认为错误不仅仅止于此,new_cuckoo_hash_table中的所有变量都可能被错误地处理.

我理解这与我没有为我的table1声明类型有关,但让我困惑的是,我有一个包含InnerTable *table1的结构,希望有人能指出这个错误的原因。

如有任何更正,将不胜感激!

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "cuckoo.h"

typedef struct inner_table {
    int64 *slots;   // array of slots holding keys
    bool  *inuse;   // is this slot in use or not?
} InnerTable;

// a cuckoo hash table stores its keys in two inner tables
struct cuckoo_table {
    InnerTable *table1; // first table
    InnerTable *table2; // second table
    int size;           // size of each table
};


// initialise a cuckoo hash table with 'size' slots in each table
CuckooHashTable *new_cuckoo_hash_table(int size) {
    InnerTable table1;
    assert(size < MAX_TABLE_SIZE && "error: table has grown too large!");
    table1 -> slots = malloc((sizeof *table1->slots) * size);
    assert(table1->slots);
    table1->inuse = malloc((sizeof *table1->inuse) * size);
    assert(table1->inuse);
    return NULL;

    //return NULL;
}

编辑:

对于那些需要更多关于CuckooHashTable的信息的人

代码语言:javascript
复制
typedef struct cuckoo_table CuckooHashTable
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-10 09:25:51

结果,我必须初始化另一个指针,用于指向table1table2

如下所示:

代码语言:javascript
复制
newtable->table = malloc(sizeof (*newtable->table));

然后以类似的方式开始研究slotsinuse。希望这能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2017-05-09 15:54:28

所以你可能想要这样的东西(只是从评论中猜测,看看我的水晶球):

代码语言:javascript
复制
CuckooHashTable *new_cuckoo_hash_table(int size) {
    assert(size < MAX_TABLE_SIZE && "error: table has grown too large!");

    CuckooHashTable *newtable = malloc(sizeof(CuckooHashTable));
    assert(newtable);
    newtable->table1 = malloc(sizeof(InnerTable));
    assert(newtable->table1);
    newtable->table1->slots = malloc((sizeof *table1->slots) * size);
    assert(table1->slots);
    newtable->table1->inuse = malloc((sizeof *table1->inuse) * size);
    assert(table1->inuse);

    return newtable;
}

这是未经测试的,没有错误的检查代码,可能有排印和代码可能无法编译。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43874156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档