diff --git a/ex22.c b/ex22.c index 922ec89..df5612c 100644 --- a/ex22.c +++ b/ex22.c @@ -13,12 +13,29 @@ int get_age() } +int *get_age_pointer() +{ + return &THE_AGE; +} + + void set_age(int age) { THE_AGE = age; } +double *get_function_static(double to_add) +{ + static double value = 1.0; + + double old = value; + value = value + to_add; + + return &value; +} + + double update_ratio(double new_ratio) { static double ratio = 1.0; diff --git a/ex22.h b/ex22.h index 01b93ff..c89665f 100644 --- a/ex22.h +++ b/ex22.h @@ -6,6 +6,8 @@ int THE_SIZE; // gets and sets an internal static variable in ex22.c int get_age(); +int * get_age_pointer(); +double * get_function_static(double); void set_age(int age); // updates a static variable that's inside update_ratio diff --git a/ex22_main.c b/ex22_main.c index 39502e5..7b608e2 100644 --- a/ex22_main.c +++ b/ex22_main.c @@ -31,6 +31,13 @@ int main(int argc, char *argv[]) log_info("My age is now: %d", get_age()); + int new_age = 44; + log_info("Setting age directly to %d", new_age); + int *age = get_age_pointer(); + *age = new_age; + + log_info("My age is now: %d", get_age()); + // test out THE_SIZE extern log_info("THE_SIZE is: %d", THE_SIZE); print_size(); @@ -45,6 +52,15 @@ int main(int argc, char *argv[]) log_info("Ratio again: %f", update_ratio(10.0)); log_info("Ratio once more: %f", update_ratio(300.0)); + // accessing a function static + double *func_static_value = get_function_static(4.0); + log_info("get_function_static(4.0) = %.1f", *func_static_value); + double new_func_static_value = 8.0; + log_info("Setting the function static var directly to %.1f", + new_func_static_value); + *func_static_value = new_func_static_value; + log_info("get_function_static(4.0) = %.1f", *get_function_static(4.0)); + // test the scope demo int count = 4; scope_demo(count);