* * Project 4 due December 13th * Final Exam is December 17th at 8:00 in 0135 EGR * Complete course evaluations! * * Unions: * Remember: unions ARE NOT structs- they can only take on 1 value * Ex. * union union_test{ char single_char; char *mult_char; int single_int; int *mult_int; } * * Accessing union values: * The syntax looks just like it does for structs: union_test.single_char=‘a’; union_test.mult_char=“Hello!”; * Remember: the struct can only take one value at a time * Consider keeping track of what kind of data is inside the union, ex. poly-print.c * **Hint** working with unions and exploiting the fact that they can only have one value at a time could be a part of a question on the final… * * Void pointers: * Ex. void *mystery_data; * Widely expand the use of an ADT * Allows you to create an ADT that operates on some unknown type of data. * Tips: * Don’t flip when you see a void **! (or a void ***) void *data_field; int extra; Char extra2; * ??? * Adds another level of flexibility * Fantastic for loops with different conditions! * Structure of function pointer is just like function declaration: <output>(*<function pointer name>)(<input1>,<input2>,<input3>); * Ex. void (*insert_func)(int *, int, int); * * Another idea! * What if we embed function pointers in an ADT? * Ex. when a new struct variable is initialized you could set that node’s functions as well. * Warning: the syntax is terrible! * typedef struct new_data_{ int data1; char data2; struct new_data_ *next; int print_func(struct new_data_*); }new_data; * * Two parts to the project: * Backend: create the ADT * Use the ADT to implement a dictionary * Pay close attention to the function specifications * * Yes, abstract data types DO exist in C. * Data type includes: * Some type of data or representation of data * Could be: primitives, structs, unions, void pointers * Specific functions to operate on the data * typedef struct rational_ { int numerator; int denominator; } rational; Just a struct rational new_rat(int numerator, int denominator); rational add_rat(rational x, rational rational sub_rat(rational x, rational rational mul_rat(rational x, rational rational div_rat(rational x, rational void print_rat(rational x); y); y); y); y); Functions to be used with the struct * * Someone OTHER than the author needs to be able to use the ADT * Ex. the best way to REALLY test project 4, give your files to a friend and ask them to code with it! * Includes a full set of functions to use the datatype * Ex. Add, subtract, multiply (for integer like datatypes), store, print, find (for memory arrangements) * * Hiding functions that you DON’T want nonauthors to touch * Some functions could do damage to different aspects of a program’s implementation if called incorrectly * Use ‘static’ to restrict functions to the .c module in which it’s declared * Then it can’t be called from any other .c file! * * From * http://www.cs.princeton.edu/courses/archive/spr9 6/cs217/precepts/adt-precept/ * Note: This program uses 3 different files * Header: defines the struct and the different functions provided * Function .c module: includes the code for all of the functions * Function with main: makes use of the ADT designed in the other two files *