I have a problem that involves the use of a structure and pointers (not allowed to use arrays). I have to add a record using pointers and memory allocation and then eventually delete records. I do not know how to use memcpy to copy the structure itself to add to the program.
I have a structure:
struct childrensBook //Structure of books using pointers instead of arrays
{
char *title; //use of pointers to store memory for title
char *author; //use of pointers to store memory for author
char *publisher; //use of pointers to store memory for publisher
int copyright;
double price;
};
int moveptr; //moveptr would be used to move to the next structure.
struct childrensBook *book1 = (struct childrensBook *) malloc(sizeof(s truct childrensBook)) ; //Structure of book #1
book1->title = (char *)malloc(100); //allows memmory of 100 characters
book1->author = (char *)malloc(100); //allows memmory of 100 characters
book1->publisher = (char *)malloc(100); //allows memmory of 100 characters
book1->copyright = 1997;
book1->price = 7.99;
memcpy(book1->title, "We're Going on a Bear Hunt", 26);
memcpy(book1->author, "Michael Rosen", 13);
memcpy(book1->publisher, "Little Simon", 12);
book1->copyright = 1989;
book1->price = 7.99;
book1++;
Then I have an "add" function that I am trying to create where it will copy the structure. This is what I can't figure out.
static int addRecord(struc t childrensBook book1, char *title, char *author, char *publisher, int copyright, double price)
{
int headptr = book1;
memcpy(book1, sizeof(struct childrensBook)) ;
}
I have a structure:
struct childrensBook //Structure of books using pointers instead of arrays
{
char *title; //use of pointers to store memory for title
char *author; //use of pointers to store memory for author
char *publisher; //use of pointers to store memory for publisher
int copyright;
double price;
};
int moveptr; //moveptr would be used to move to the next structure.
struct childrensBook *book1 = (struct childrensBook *) malloc(sizeof(s truct childrensBook)) ; //Structure of book #1
book1->title = (char *)malloc(100); //allows memmory of 100 characters
book1->author = (char *)malloc(100); //allows memmory of 100 characters
book1->publisher = (char *)malloc(100); //allows memmory of 100 characters
book1->copyright = 1997;
book1->price = 7.99;
memcpy(book1->title, "We're Going on a Bear Hunt", 26);
memcpy(book1->author, "Michael Rosen", 13);
memcpy(book1->publisher, "Little Simon", 12);
book1->copyright = 1989;
book1->price = 7.99;
book1++;
Then I have an "add" function that I am trying to create where it will copy the structure. This is what I can't figure out.
static int addRecord(struc t childrensBook book1, char *title, char *author, char *publisher, int copyright, double price)
{
int headptr = book1;
memcpy(book1, sizeof(struct childrensBook)) ;
}
Comment