structure question

hy , i have the following exercise: to realise a soft in C, like a database, to be able to add delete search etc. The data are;
artistic movie: title, director,year,lenght of the movie,borrowed (yes or no and if yes then i have to display the name of the person, date when it was borrowed, and date of return (year,month,day)).

My question is this, i have to make artistic movie a structure right ?but i dont know what Borrowed is, because it has elements too (yes, no, name, date etc)

[code]
typedef struct {
char Title[100];
char Director[50];
int Year;
double lenght;
char Borrowed[]
} artistic_movie;
[/code]

Comments

  • : hy , i have the following exercise: to realise a soft in C, like a
    : database, to be able to add delete search etc. The data are;
    : artistic movie: title, director,year,lenght of the movie,borrowed
    : (yes or no and if yes then i have to display the name of the person,
    : date when it was borrowed, and date of return (year,month,day)).
    :
    There's a problem with databases, which is what to do when the data is naturally of variable length. For instnace several people might have borrowed and returned your movie during its library life, and it is not sensible to specify a maximum.

    Internally C handles this with the malloc() statement. If we specify a struct "Borrowed" with two dates - date of borrowing nad date of return, we can then say

    movie->borrowings = malloc(Nborrowed * sizeof(struct Borrowed));

    However things are a bit more difficult when you put them into a database. What a modern relational database will do is declare the movie::borrowings relationship as a one to many. So each "borrowing" entry needs the name of the movie it refers to. This is declared as a "foreign key". They keys are then indexed so you don't have to trawl the entire dataset of borrowings to pull out all borrowings of "Gone with the Wind".

    However if you are implementing you own databse you might want a much simpler approach, which is to simply save a field "N times borrowed" and then save N borrowing records. This is easily done. When you come to read the data you read in the "N times borrowed" field first, then that tells you how many borrowing fields follow.

  • hmmz i did a little bit of SQL in hightschool and i am aware of "foreign key" and "one to many". Unfortunately i didnt learn it :(.

    Being a beginner i didnt quite understand everything u said.
    So i have to make another struct named "Borrowed" with 2 fields: date of borrow and date of return , right ?
    Can u please explain a bit this statement
    [code]movie->borrowings = malloc(Nborrowed * sizeof(struct Borrowed));[/code]
    -> as i know it has something to do with pointers , so movie points to another struct borrowings ? , which everytime i borrow it allocates the memory ?
    Nborrowed = how many times i borrowed ?

    You also said here to create a field "N times borrowed and then save N borrowing records". Where do i create these fields, in which structure. Borrowed ?

    Sorry but i am a newbie and i want to properly understand.Also can u direct me to a tutorial about this in C.Thanks in advance
  • : hmmz i did a little bit of SQL in hightschool and i am aware of
    : "foreign key" and "one to many". Unfortunately i didnt learn it :(.
    :
    : Being a beginner i didnt quite understand everything u said.
    : So i have to make another struct named "Borrowed" with 2 fields:
    : date of borrow and date of return , right ?
    :
    That's right. Each borrowing has some data associated with it, like date borrowed, date returned, maybe the name of the borrower. So you need to set that up in a struct.

    However another bit of data is the identity of the book. When we store the data in RAM, normally we attach a list of borrowings to a book in the following manner.

    typedef struct
    {
    ... /* title and other book data here */
    int Nborrowed; /* number of times it has been borrowed */
    struct Borrow *borrowed; /* the borrowing data */
    } BOOK;

    Since the book could have been borrowed zero or many hundreds of times, we don't know how big Nborrowed is at compile time. So we don't know how big to make the buffer that the "borrowed" member points to. The answer is to use malloc(). malloc() will give us a buffer big enough to put all our data in.


    When the data is saved out to disk, often we do things a little differently, because you cannot save pointers. Then we'd usually to tag each borrowing with the id of the book that it refers to (a foreign key), and rebuild the list.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories