开发者

Accessing values inside a structure in Berkeley DB using C

开发者 https://www.devze.com 2023-03-31 03:23 出处:网络
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.

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.


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) :

key.data = (struct pearson_key *) &keyStruct;

to access the value :

Value = (key.data)->k


It should be :

printf("value = %d", keyStruct.k);

Take the time to read up on C structures, pointers and printf syntax.

struct pearson_key{ 
  int k;
};
struct pearson_key keyStruct; 
DBT key;
memset(&key, 0, sizeof(key));
memset(&keyStruct, 0, sizeof(struct pearson_key));
keyStruct.k = 1;
key.data = &keyStruct;
key.size = sizeof(keyStruct);
printf("value = %d", keyStruct.k);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号