Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <string.h>
- #define BUF_LEN 20 // for now, maybe will try something different later
- #define MAX_PPL 100
- /******************************************************************************************************************
- Developer: My Name
- Goal: Learning about the following:
- - Structures, and typedefing
- - Function pointer members
- - Memory allocation
- Summary of code:
- I am creating a database of people to keep track of. There is a structure (type data_header) that manages
- a database of a maximum of 100 people (data_header->people_list[MAX_PPL]).
- - note for now, there is a maximum amount and it is not a dynamically allocated value.
- Architecture of structures:
- - data_header
- +- people currently tracking ('used')
- +- people_list[MAX_PPL]
- | +- list of pointers that point to person_t type structures
- | +- person:
- | +- Age
- | +- Height structure pointer:
- | | +- feet
- | | +- inches
- | +- name
- | +- hair_color
- | +- eye_color
- +- Function handles for debugging/querying purposes
- There are several data structure initialization routines. They each return a pointer to the beginning of the
- storage allocated in memory for the structures. These pointers stay valid, as malloc is not concerned
- with scoping.
- ******************************************************************************************************************/
- typedef struct height_t{
- uint8_t feet;
- uint8_t inches;
- }height;
- typedef struct person_t{
- uint8_t Age;
- height *Height;
- char name[BUF_LEN];
- char hair_color[BUF_LEN];
- char eye_color[BUF_LEN];
- }person;
- /* data_header manages the structures.
- list head
- list tail
- number of items
- base address of an array
- etc...
- What's the purpose of 'static' members in a struct?
- Typically for an array: base address, number of items allocated, number used so far, sizeof struct,
- address of compare routine (for bsearch and qsort),
- maybe address of a debug print function for elements
- */
- typedef struct data_header_t{
- uint8_t used;
- //ssize_t struct_size;
- person * people_list[MAX_PPL];
- // function addresses
- void (*debug_all)(struct data_header_t *);
- void (*debug_person)(person *);
- uint8_t (*query_person)(struct data_header_t *, char *);
- }data_header;
- void debug_all(data_header * database){
- // say how many people there are,
- // list all of their names
- printf("People keeping track of:\t%d\n",database->used);
- int i;
- printf("List of names:\n");
- for(i = 0; i < database->used; i++){
- printf("%s\n",database->people_list[i]->name);
- }
- return;
- }
- void debug_person(person * JohnyBoi){
- printf("Name:\t\t%s\n",JohnyBoi->name);
- printf("Age:\t\t%d\n",JohnyBoi->Age);
- printf("Height:\t\t%d\'%d\"\n",JohnyBoi->Height->feet,JohnyBoi->Height->inches);
- printf("Hair color:\t%s\n",JohnyBoi->hair_color);
- printf("Eye color:\t%s\n",JohnyBoi->eye_color);
- return;
- }
- uint8_t query_person(data_header * database, char * query){
- uint8_t i;
- for(i=0;i<database->used;i++){
- if(strcmp(database->people_list[i]->name,query) == 0)
- return(i);
- }
- return(-1);
- }
- // Let's say I want to be able to store 100 people in my database. Need to allocate the space for 100 people structures
- height * height_init(uint8_t feet, uint8_t inches){ // returns a pointer for a height structure
- height *p = (height*)malloc(sizeof(height));
- p->feet = feet;
- p->inches = inches;
- return p;
- }
- person * person_init(char* name, char* hair_color, char* eye_color, uint8_t age, uint8_t feet, uint8_t inches){ // returns a pointer for a person structure
- person *p = (person*)malloc(sizeof(person));
- p->Age = age;
- p->Height = height_init(feet,inches);
- memcpy(p->name,name,strlen(name)+1);
- memcpy(p->hair_color,hair_color,strlen(hair_color)+1);
- memcpy(p->eye_color,eye_color,strlen(eye_color)+1);
- return p;
- }
- data_header * data_header_init(){
- // need to allocate the space for 100 person structures
- // each person structure also needs to allocate the space for itself + Height (since it's a pointer!)
- data_header *p = (data_header*)malloc(sizeof(data_header)); // note: right now since it won't be dynamic, there is already allocated buffer for max people.
- // that means we have already allocated the space for storing the pointers.
- // if we want to actually use those pointers, we need to allocate the space for the structures they can point at
- p->debug_all = debug_all;
- p->debug_person = debug_person; // returns the function handle/pointer to the member p->debug_person
- p->query_person = query_person;
- return p;
- }
- // include the input for: name, age, height, hair_color, eye_color
- int8_t add_person(data_header * database, char *name, char *hair_color, char *eye_color, uint8_t age, uint8_t feet, uint8_t inches){ // need to
- if(database->used == 100)
- return(-1);
- database->people_list[database->used++] = person_init(name,hair_color,eye_color,age,feet,inches);
- return(0);
- }
- void deallocate_data_header(data_header * database){
- // first, need to go thru all of the used structures to deallocate them, then can deallocate the entire thing
- int i;
- for(i=0;i<database->used;i++){
- // deallocating a person:
- // - deallocate height structure
- // - deallocate person structure
- free(database->people_list[i]->Height);
- free(database->people_list[i]);
- }
- // have deallocated every person in the database
- free(database);
- return;
- }
- int main(){
- data_header * My_Database = data_header_init();
- // create Johnny: brown hair, blue eyes, 23 years old, 5'11"
- if(add_person(My_Database,"Johnny","Brown","Blue",23,5,11))
- return(-1); // error
- // My_Database->debug_person(My_Database->people_list[0]);
- (void)add_person(My_Database,"Sara","Blonde","Brown",41,5,3);
- // My_Database->debug_person(My_Database->people_list[1]);
- int i;
- for(i = 0; i < My_Database->used; i++){
- My_Database->debug_person(My_Database->people_list[i]);
- printf("\n");
- }
- printf("\n");
- My_Database->debug_all(My_Database);
- printf("\n");
- uint8_t tmp;
- printf("Query: Johnny\nFound?\n%s\n",((tmp=My_Database->query_person(My_Database,"Johnny")) == ((uint8_t)(-1)))?"No.":"Yes.");
- // Deallocation routine
- deallocate_data_header(My_Database);
- printf("\nPost deallocation:\n\n");
- printf("Pointer for database:\t%p\n",My_Database);
- My_Database->debug_all(My_Database);
- return(0); // success
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement