90 likes | 97 Views
Explore pointers, pointer terminology, memory addresses, and how to use pointers effectively in C programming. Learn to pass multiple values with pointers and avoid global variables.
E N D
Agenda • Perform Quiz#3 (15 Minutes) • Introduction to Pointers • What are pointers? / Why use pointers? • Pointer Terminology • Memory Address format specifier • Pointer Variable • De-referencing a Pointer • Indirection • Examples
Consider In Class Exercise #2From Previous Class: • #include <stdio.h> • #define GST 7.0; • #define PST 8.0; • double Calc_Taxes (double); • int main (void){ • int quantity = 10; /* Declare Variables */ • double price = 2.25, amount, taxes, total_amount; • amount = quantity * price; /* Calculate amount, taxes & total */ • taxes = Calc_Taxes (amount); • total_amount = amount + taxes; • printf ("\nSales:\t$%.2lf\tTaxes:\t$%.2lf\tTotal:\t$%.2lf\n\n", amount, taxes, total_amount); • return 0; • } • double Calc_Taxes (double amt){ • double pst, gst, total_taxes; • pst = amt * (PST / 100); /*Calculate PST and GST & Total Taxes */ • gst = amt * (GST / 100); • total_taxes = pst + gst; • return total_taxes; • } Function Calc_Taxes was used to calculate and return total taxes to the main program.
What if We Want to ReturnBoth PST and GST Values? Let’s take a closer look at the Calc_Taxes function: • double Calc_Taxes (double amt, double pst, double gst){ • double total_taxes; • pst = amt * (pst / 100); /*Calculate PST and GST & Total Taxes */ • gst = amt * (gst / 100); • total_taxes = pst + gst; • return total_taxes; • } The variables pst and gst are local to the Calc_Taxes function only, the only value in this function that is returned is total_taxes
How to Solve the Problem? • There are two methods to have more than one variable passed-back when running a function: • Method #1 – Use Global Variables • This method is not recommended since many people may work on different functions and not be aware of global variable. They may change global variable without realizing effects from change in entire program. • Method #2 – Use Pointers • Use pointers as a “trick” to point to the memory address of an already existing variable. • This method is recommended since programmers can create their own variables that will not affect global variables, thus affecting the entire program.
What are Pointers? • Pointer: • A pointer is a value or variable that contains the memory address that stores a value of another variable. • This provides a “trick” to change the values of variables within other functions
Steps to Use Pointers • Step 1: Declare a Pointer • The pointer is declared (usually in the function header). • An asterisk “*” (the indirection operator) in front of a variable name is used to access a specific memory location of a variable. • To avoid confusion with a pointer, it is recommended to place a “p” or “ptr” to indicate that a variable is a pointer.eg. int *p_gst, *p_gst;
Steps to Use Pointers • Step 2: Assign Memory Address to Pointer • In order to use a pointer, we must assign the memory address of another variable to the pointer variable. • eg. *p_gst = &gst; *p_pst = &pst; • Calc_Taxes (amount, *p_pst, *g_gst); • When using functions, it may be easier to pass the memory address to the function instead: • eg. Calc_Taxes (amount, &gst, &pst);
Steps to Use Pointers • Step 3: Use a Pointer to change Original Variable • When calling a function, the value of the memory address is passed to a pointer variable. • Within the function, the pointer variable is used to change the value of the original variable • eg. int *p_gst, *p_gst;
Solution to ProgramUsing Pointers • #include <stdio.h> • #define GST 7.0 • #define PST 8.0 • void Calc_Taxes (double, double *, double *); • int main (void){ • int quantity = 10; • double price = 2.25, amount, taxes, total_amount; • double pst, gst; • amount = quantity * price; • Calc_Taxes (amount, &gst, &pst); • taxes = pst + gst; • total_amount = amount + taxes; • printf ("\nSales:\t\t$%.2lf\n\n", amount); • printf ("Taxes:\n"); • printf (" GST:\t\t$%.2lf\n PST:\t\t$%.2lf\n", gst, pst); • printf ("Total Taxes:\t$%.2lf\n\n", taxes); • printf ("Invoice Total:\t$%.2lf\n\n", total_amount); • return 0; • } • void Calc_Taxes (double amt, double *p_gst, double *p_pst){ • *p_pst = amt * (PST / 100); • *p_gst = amt * (GST / 100); • } Notice that with Pointers, we didn’t have to to return any value from the function!!