c - 在 C 中创建具有 >1byte 键和值的哈希表
问题描述
我正在尝试从头开始在 C 中创建一个哈希表。这是一个带有char*
char key[32], char value[32]
1字节(这是我的struct
:
#define KV_SIZE 32
typedef struct hash_entry{
char key[KV_SIZE];
char value[KV_SIZE];
struct hash_entry* next;
} hash_entry;
我无法形成一个名为的函数,create_entry()
因为我不知道如何将我的struct
字符串、键和值分配给值。
// create an entry
hash_entry* create_entry(char key[KV_SIZE], char value[KV_SIZE]){
printf("%s\n", key);
hash_entry* entry = (hash_entry*)malloc(sizeof(hash_entry*));
// I want entry->key and entry->value to store a string up to 32 chars long
strncpy(entry->key, key, strlen(key)); // Error
strncpy(entry->value, value, strlen(value)); // Error
entry->next = NULL;
return entry;
}
到目前为止,似乎我需要将我entry
的 's 保持声明为指针 ( hash_entry* entry
) 而不是非指针 ( hash_entry entry
) 以便以后能够链接它们。
解决方案
hash_entry* entry = (hash_entry*)malloc(sizeof(hash_entry));
推荐阅读
- javascript - 如何使用javascript实现ios safari浏览器的打开相机和放大/缩小功能
- algorithm - 是否有任何有效的算法可以找到无向图中最长循环的长度?
- reactjs - 关于防止对 react/redux 进行不必要的重新渲染的思想实验反馈
- amazon-web-services - 运行 AWS Redshift/获取/保留它的快照是否在内部运行 EC2 实例?
- ios - AppCenter 构建错误“配置文件不包括签名证书苹果推送服务”
- python - 将充满时间增量的numpy数组转换为小时
- macos - 运行 mix phx.server 时找不到可执行的“mac_listener”
- javascript - 如果在 Firefox 浏览器中放置下一个,lightgallery 将关闭
- python - 如何将 iot 设备连接到我电脑中的 django 服务器?
- php - VueJS axios 上传图片