IPC 144 Introduction to Programming Using C Week

  • Slides: 9
Download presentation
IPC 144 Introduction to Programming Using C Week 6 – Lesson 1 (Pages 47

IPC 144 Introduction to Programming Using C Week 6 – Lesson 1 (Pages 47 to 51 in IPC 144 Textbook)

Agenda Additional C programming Elements Pointers Purpose Passing Pointers to Functions Indirection Example

Agenda Additional C programming Elements Pointers Purpose Passing Pointers to Functions Indirection Example

Modularity We have learned in previous lessons, that functions allow us to break-down programs

Modularity We have learned in previous lessons, that functions allow us to break-down programs into smaller elements. We can pass parameters to functions, and have a value returned from a function… There is a problem: Functions can only return one value, not multiple values…

Pointers are a method or “trick” to be able to pass up the memory

Pointers are a method or “trick” to be able to pass up the memory address of variables, and allow the user to change the value of the variable via its memory address. Memory address of variables Values of variables changed in main ( ) for example…. Function Change values of variables within Function (Through a process called indirection …)

Pointers First, the function header and prototype should indicate which parameter(s) are a “pointer”

Pointers First, the function header and prototype should indicate which parameter(s) are a “pointer” or “memory address” to variable. This is accomplished by placing an asterisk before the variable… eg. void function. Name (double x, int *y) Indicates a “cast” i. e. parameter is accepting a “Pointer” or memory address location of variable called y ….

Pointers Second, when the function that accepts pointers is called, you must include an

Pointers Second, when the function that accepts pointers is called, you must include an “&” symbol before the variable name that is going to represent the pointer or “memory address” to that variable Using the previous slide’s example, being called with main (): function. Name (x, &y); Note: second variable used as a pointer. Just using the same names in main() for variables to prevent confusion…

Pointers Third, within the function, to change the original value of the variable (passed

Pointers Third, within the function, to change the original value of the variable (passed up as a parameter to your function as a pointer), you use indirection. You simply place an asterisk in front of the variable to make reference to it’s original value. Using the previous slide’s example, now within the function called “function. Name”: *y = *y + x; For example: original y in main is equal to original value of y + x

Example #include <stdio. h> void hours_and_minutes(int total, int *phrs, int *pmin); main() { int

Example #include <stdio. h> void hours_and_minutes(int total, int *phrs, int *pmin); main() { int tmin, hr, min; printf (“Enter total minutes: “); scanf (“%d”, &tmin); hours_and_minutes(tmin, &hr, &min); printf (“%d hours, and %d minutesn”, hr, min); } void hours_and_minutes(int total, int *phrs, int *pmin) { *pmin = total % 60; *phrs = total / 60; }

Homework TASK #1 TASK #2 Complete lab #5 since it is due at the

Homework TASK #1 TASK #2 Complete lab #5 since it is due at the beginning of this week’s lab! Study for test #1 – to be held in week #7 TASK #4 *** Highly Recommended *** Read ahead in IPC 144 Programming Notes (Filling in the Gaps).