【发布时间】:2017-01-16 18:12:53
【问题描述】:
我会尽量保持简短。 所以我有两个结构:
typedef struct someStruct named;
struct someStruct {
void *key;
void *value;
};
typedef struct anotherStruct namedToo;
struct anotherStruct {
named **table;
unsigned int size;
};
好的,现在忽略上面所有可能的错误,这是不可编辑的代码。
现在我有两种方法:
namedToo *init(float ignoreThis) {
namedToo *temp;
temp = malloc(sizeof(namedToo)); //some memory for the second struct
temp->table = malloc(sizeof(named)*100); //lets say this 100 is for 100 buckets/cells/named elements
return temp;
方法二:
int insert(namedToo *temp, void* key, void* value) {
temp->table[0]->key = key; //My problem, I need to access the value and key inside named from the pointer inside namedToo. How can I do this?
}
评论有我的问题: 我的问题,我需要从namedToo中的指针访问named中的值和键。我怎样才能做到这一点?我需要不时自己更改和获取值/键。
【问题讨论】:
-
名称
table表明它应该被视为二维数组,而不是一维数组。是这样吗? -
它有可能,但不是必须的,table 的原因是因为它是一个哈希表/映射的残余。第一个结构中有一个指针,它指向另一个用于单独链接的元素,尽管我没有在这里包含它。让我们假设使用的名称是完全任意的。 @dbush
-
init没有正确地将table初始化为指向指针的指针。
标签: c arrays pointers struct hashmap