C program for College

Hi guys,

So i have this project i have to do in 6 weeks and i'm having some trouble in finding a good Struct to put the data in.

The project is about authors and years. Our professor gave us a 330.000 line input that has in each line something like this:

"Yan Lei, Xiaoguang Mao, Ziying Dai, Chengsong Wang, 2012"

"Shouichi Nagano, Yusuke Ichikawa, Toru Kobayashi, 2012"

 

Where you have 1 or more first and last names and the year in which the book was written.

What kind of structure do you think is better?

Me and my partner were thinking of an array of structs where the structs had the name of one author and then a pointer to an array which had who said writer worked with and when.

an array would probably be the best.

I would actually use two different structs and an array of structs:

struct Author
{

  char *firstName;

   char *lastName;


} ;

struct Book
{
   

   unsigned short year;


    struct Author *authors;

};

Don't forget to allocate memory for both structs for each book


Here's an example for one book

struct Book book = malloc(sizeof(*book) * sizeof(*book->authors));

And you will have to allocation memory for the first and last names of each author's first and last names and store them in each struct as you read them in.

int someLength = 30;


char * firstName = (char *) malloc((someLength+1) * sizeof(char);


that hurt my eyes.

you need to store the array size, you don't cast allocations, malloc returns a pointer, your book allocation doesn't make sense if authors is an array because that would always give you a one element array.

Yeah, I think I said that in my comment.  

I was giving an example for one book and one author.

I am not going to give the code for dynamically allocating and re-allocating both the author array and book array when encountering different sizes of each.

That would be the bulk of their assignment and I am sure they want to do that on their own.