使用 C 访问 Berkeley DB 结构内部的值
我想要 Berkeley DB 中的“key.data”有一个整数值。由于我们在 Berkley DB 中使用 DBT 结构,并且它具有“指向字节字符串的指针”,因此我使用 memeber int 创建了一个 key 结构。但现在我在访问结构内存储的值时遇到问题。下面是我的代码:
struct pearson_key{
int k;
};
struct pearson_key keyStruct;
DBT key
memset(&key, 0, sizeof(key));
memset(&keyStruct, 0, sizeof(struct pearson_key));
int k = 1;
keyStruct.k = k;
key.data = &keyStruct;
printf("value = %s",(char*)keyStruct);
key.size = sizeof(keyStruct);
它正在打印空白值。我是 C 语言和结构的新手。我知道我的结构有问题,但不知道如何纠正。提前致谢。
I want to have a integer value to my "key.data" in Berkeley DB. Since we use DBT structures in Berkley DB,and it has "A pointer to a byte string", I created a structure for key with a memeber int. But now I am facing problem in accessing the value stored inside structure. Below is my code:
struct pearson_key{
int k;
};
struct pearson_key keyStruct;
DBT key
memset(&key, 0, sizeof(key));
memset(&keyStruct, 0, sizeof(struct pearson_key));
int k = 1;
keyStruct.k = k;
key.data = &keyStruct;
printf("value = %s",(char*)keyStruct);
key.size = sizeof(keyStruct);
It is printing blank value. I am new to C and structures. I know I am somewhere wrong with structures, but don't know how to rectify it. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我是正确的,您想通过您的
key
访问整数值。现在,您的密钥有一个指向字节字符串的指针。我不太确定,我认为它可能是一个空指针(void *)
,这样它就可以指向任何类型的数据。无论如何,您可以执行以下操作(假设我上面所说的是真的):
访问该值:
If I am correct, you want to access and Integer value through your
key
. Now, your key has a pointer to a byte string. I am not so sure, I think it could be a void pointer(void *)
, so that it could point to data of any type.Anyway you can do the following (assuming what I said above is true) :
to access the value :
应该是:
花时间阅读 C 结构、指针和 printf 语法。
It should be :
Take the time to read up on C structures, pointers and printf syntax.