////////////////////////// // // prog_practice_6 project // // 9/19/22 tj // // practice pointers with functions // //////////////////////////// #include "mbed.h" #include void splash(void); void read_vals(int* val1, int* val2); float swap_ave(int* v1, int* v2); void calc_ave(int v1, int v2, float * result); float * point_2x(float * ref); int main(void){ setbuf(stdout, NULL); // fix buffer problem // splash splash(); int a; int b; float c; float * d_ptr; // show garbage printf("%i %i %f %p %p %p\n", a, b, c, &a, &b, &c); // read in 2 numbers with scanf printf("please enter 2 ints: "); scanf("%i %i", &a, &b); printf("%i %i %f %p %p %p\n", a, b, c, &a, &b, &c); // read in 2 numbers from our own function read_vals(&a, &b); printf("%i %i %f %p %p %p\n", a, b, c, &a, &b, &c); // swap values AND provide the ave c = swap_ave(&a, &b); printf("%i %i %f\n", a, b, c); // cal ave using calc_ave calc_ave(a, b, &c); printf("%i %i %f\n", a, b, c); // show that we can return a pointer // we could have just said d_ptr = &c // we will not return pointers in this class d_ptr = point_2x(&c); printf("%f %p %f\n", c, d_ptr, *d_ptr); return 0; }// end main void splash(void){ // print splash message printf("\n\nPractice using pointers in functions\n\n"); return; }// end splash void read_vals(int* val1, int* val2){ printf("\nEntered read_vals\n"); printf("val1, val2: %p %p\n", val1, val2); // read in 2 values and store via address reference // note: scanf is already expecting addresses(pointers) so you // can use val1 and val2 directly in the scanf function printf("please enter an integer value: "); scanf("%i", val1); printf("please enter an integer value: "); scanf("%i", val2); return; }// end read_vals float swap_ave(int* v1, int* v2){ printf("\nEntered swap_ave\n"); printf("v1, v2: %p %p\n", v1, v2); // swap two values and calculate/return the average int tmp; tmp = *v1; *v1 = *v2; *v2 = tmp; return ((*v1 + *v2)/2.0); }// end swap_ave void calc_ave(int v1, int v2, float * result){ printf("\nEntered calc_ave\n"); printf("v1, v2, result: %i %i %p\n", v1, v2, result); // calculate the average *result = (v1 + v2)/2.0; return; }// end calc_ave float * point_2x(float * ref){ //return the pointer passed to the function return ref; }// end point_2x