/* * programming_structures_fns.c * * Created on: Apr 14, 2023 * Author: johnsontimoj */ ////////////////////////////////// // // practice with structures in functions // // inputs - user // outputs - prints // ////////////////////////// #include //////////////////////// // structs DEFINED in global area so all functions // know about the new type // Note: we Do NOT create structs here /////////////////////// // Define a structure using tag /////////////////// // structure: vacation // // holds a number and travel distance for a vacation trip /////////////////// struct vacation{ int vac_no; float vac_dist; }; // Define a new type that is a structure //////////////////// // structure: trip // // holds the mpg and type of trip (w- work, f - family, e - errand) //////////////////// typedef struct{ float mpg; char trip_type; }trip; // Function Prototypes - After structure definitions so they know what the structures are void print_vacation(struct vacation my_vac); void print_vacation_ptr(const struct vacation * vac_ptr); void print_trip(trip my_trip); void print_trip_ptr(const trip * trip_ptr); trip load_trip(void); int main(void){ setbuf(stdout, NULL); // fix buffer problem // splash - no function to save time printf("\n\nStructure program with Functions\n\n"); // create a new structure using the tag approach struct vacation yosemite; yosemite.vac_no = 3; yosemite.vac_dist = 1234.5; print_vacation(yosemite); // use a pointer to pass the structure print_vacation_ptr(&yosemite); // create a new structure using the typedef approach trip work; work.trip_type = 'w'; work.mpg = 32.35; print_trip(work); // use a pointer to pass the structure // note: skip creating a separate variable by using &work print_trip_ptr(&work); // load a new structure using a function trip new_trip; new_trip = load_trip(); print_trip(new_trip); return 0; }// end main //////////////////////////////////// // print_vacation // // prints a structure of type vacation // // inputs: the structure // outputs: print ////////////////////////////////// void print_vacation(struct vacation my_vac){ printf("I traveled %f miles on vacation number %i\n", my_vac.vac_dist, my_vac.vac_no); return; }// end print_vacation //////////////////////////////////// // print_vacation_ptr // // prints a structure of type vacation via a pointer // // inputs: pointer to the structure // outputs: print ////////////////////////////////// void print_vacation_ptr(const struct vacation * vac_ptr){ printf("I traveled %f miles on vacation number %i\n", vac_ptr->vac_dist, vac_ptr->vac_no); return; }// end print_vacation //////////////////////////////////// // print_trip // // prints a structure of type trip // // inputs: the structure // outputs: print ////////////////////////////////// void print_trip(trip my_trip){ printf("I averaged %f mpg on my trip of type %c\n", my_trip.mpg, my_trip.trip_type); return; }// end print_vacation //////////////////////////////////// // print_trip_ptr // // prints a structure of type trip via a pointer // // inputs: pointer to the structure // outputs: print ////////////////////////////////// void print_trip_ptr(const trip * trip_ptr){ printf("I averaged %f mpg on my trip of type %c\n", trip_ptr->mpg, trip_ptr->trip_type); return; }// end print_vacation //////////////////////////////////// // load_trip // // requests values for and returns a structure of type trip // // inputs: none // outputs: returns a structure of type trip ////////////////////////////////// trip load_trip(void){ trip tmp; printf("enter the type of the trip (w- work, f - family, e - errand): "); scanf("%c", &(tmp.trip_type)); printf("enter the mpg for this trip: "); scanf("%f", &(tmp.mpg)); return tmp; }// end load_trip