Search code examples
cberkeley-db

Not printing proper value of "key.data" in Berkeley DB using C


I am new to Berkeley DB and learning using online guide. Now i have the following code below:

DB *dbp;
DBT key, data;
int ret, t_ret;
int k = 1;

key.data = &(k);
key.size = sizeof(k);
memset(&key, 0, sizeof(key));

if ((ret = dbp->put(dbp, NULL, &key, &data, 0)) == 0)
{
    printf("db: %d: key stored.\n", (char*)key.data);
}

Now the printf statement, instead of returning value "1", is returning something else. Don't know where I am going wrong.


Solution

  • Since key.data is a void*, you need to dereference it to get the value. Try:

    printf("db: %d: key stored.\n", *(int*)key.data);