Solution for Exercise 19-24
ex1924
#include <stdio.h> void discount(float *a); int main() { float price = 42.99; float *p; p = &price; printf("The item costs $%.2f\n",price); discount(p); printf("With the discount, that's $%.2f\n",price); return(0); } void discount(float *a) { *a = *a * 0.90; }
Notes
* The addition of the p
variable to the code doesn't really change the program. The only difference is that variable p
is passed to the function instead of the address of variable price
. It's a minor difference, but I wanted to show that pointer variables can be passed to function as well as using the &
operator to pass the address of a non-pointer variable.
* The bottom line is when a function is declared as requiring a pointer value, think of the value as an address. So when you see a prototype or function definition like this:
Recognize that time_t *tloc
is a memory address. You pass an argument to the to the time() function that's either a time_t
pointer variable or a non-pointer time_t
variable prefixed by the &
operator. Or you can use the NULL
constant, as the compiler interprets that constant as a pointer value.
Copyright © 1997-2024 by QPBC.
All rights reserved