gdbm.h: noob failing to build data structure with pointer

I have been programming this simple program; I'm just trying to get data into a GDBM database and then trying to read it out. It's not complete yet, as I'm debugging it piece by piece as I write it.

bookdatabase.c:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gdbm.h>


struct bookdatum {
  char* dptr;
  int dsize;
};
typedef struct bookdatum* BookDatum;

// some prototypes i use
void set_keydatum(BookDatum key,  char* key_content);
void set_contentdatum(BookDatum content,  char* content_value);

int main(int argc, char* argv[]) {
  GDBM_FILE bookdata =  gdbm_open("/home/userhome/userbookbase.gdbm",0,GDBM_WRITER,01700,NULL);

  // build key and content objects for entry into the database
  BookDatum key1, value1;
  
  char key1_value[] = "booknumber1";
  set_keydatum(key1, key1_value );

  //set_keydatumtwo(key1, key1_value );

  char  value1_value[] = "c programming language";
  set_contentdatum(value1, value1_value); 
  //this function keeps crashing when I malloc() some data into it

  
  // send data to database
  //  gdbm_write(); etc

  //read data in database
  // gdbm_read(); etc



  gdbm_close(bookdata);
  exit(0);
}


void set_keydatum(BookDatum key,  char* key_content) {
 key->dptr =  malloc(strlen(key_content));
 strcpy(key->dptr,key_content);
 key->dsize = strlen(key_content);
 printf("keydata is: %s\n",key->dptr);
}
/*
void set_keydatumtwo(BookDatum *key,  char* key_content) {
 key->dptr =  malloc(strlen(key_content));
 strcpy(key->dptr,key_content);
 key->dsize = strlen(key_content);
 printf("keydata is: %s\n",key->dptr);
}
*/

void set_contentdatum(BookDatum content, char* content_value) {
  printf("size of content_value: %d\n", strlen(content_value)); 
  char* firstincontent = content->dptr; // content->dptr seems to be the problem
  printf("firstcontent is: %s",firstincontent);
  content->dptr =  malloc(strlen(content_value));
  printf("copying string\n");
  strcpy(content->dptr,content_value);
  content->dsize = strlen(content_value);


}

the Makefile:
Code:
bookdatabase : bookdatabase.c
	gcc -o bookdatabase bookdatabase.c -lgdbm
 
In set_contentdatum(), I get a segmentation fault at the line
Code:
 char* firstincontent = content->dptr;

I put a comment in the code, and here is the complete function in question
Code:
void set_contentdatum(BookDatum content, char* content_value) {
  printf("size of content_value: %d\n", strlen(content_value)); 
  char* firstincontent = content->dptr; // content->dptr seems to be the problem
  printf("firstcontent is: %s",firstincontent);
  content->dptr =  malloc(strlen(content_value));
  printf("copying string\n");
  strcpy(content->dptr,content_value);
  content->dsize = strlen(content_value);


}
 
Back
Top