1

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.

user537670
  • 821
  • 6
  • 21
  • 42

2 Answers2

0

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
n0nChun
  • 720
  • 2
  • 8
  • 21
  • @nOnChun: to print the value, right now i did (char*)keyStruct...will that work?? – user537670 Aug 25 '11 at 15:32
  • keyStruct is a variable of type struct, so You need to access its elements, using the . [dot] operator. Also, since you want to print an integer, either use atoi to convert the string to an integer or print using %d. But, your question needs a lot more description. Hope your peers can help you in a better way than I can. – n0nChun Aug 25 '11 at 17:15
0

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);
ScrollerBlaster
  • 1,578
  • 2
  • 17
  • 21