pacflix
The items we are storing are movies/shorts/shows/etc. And, they each need
- char title[1024]
- float rating
- float length # in minutes sure.
- int year
- char genre [1024]
int poster[384][384](an image as a 2d array!?)- int age_rating
to implement this data, we would need a seperate array for each of the above “fields” and it would look pretty ugly.
- it’ll be a pain to delete a movie
- hard to add new movies (especially since the size of arrays are constant)
We wanna bundle the fields together. duct tape yes. via Compound Data Types (CDT)
//typedef defines a new type.
// struct means it's a compound data type (many fields inside)
// show_struct is an alias for the data type
typedef struct show_struct{
char title[1024];
float rating;
int year char genre[1024];
int poster[384][384];
int age_rating;
} PacoType;
// PacoType = struct show_struct
// They are totally equivalent
int main()
{
PacoType one_paco;
one_paco.rating = 5.0;
onePaco.year = 1999;
one_paco.age_rating = 5;
// one_oaco.title = "Very Nice Family Show" is NOT allowed
strcpy(one_paco.title, "Veyr nice family show!");
strcpy(one_paco.genre, "Family");
return 0;
}
The memory model looks like this:
707 ⇒ onePaco: PacoType = junk.
708 ⇒ return = 0
Note how PacoType is just 1 box.