Presentation is loading. Please wait.

Presentation is loading. Please wait.

IPC144 Introduction to Programming Using C Week 6 – Lesson 1

Similar presentations


Presentation on theme: "IPC144 Introduction to Programming Using C Week 6 – Lesson 1"— Presentation transcript:

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

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

3 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…

4 Pointers 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 Function Values of variables changed in main ( ) for example…. Change values of variables within Function (Through a process called indirection …)

5 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 functionName (double x, int *y) Indicates a “cast” i.e. parameter is accepting a “Pointer” or memory address location of variable called y ….

6 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 (): functionName (x, &y); Note: second variable used as a pointer. Just using the same names in main() for variables to prevent confusion…

7 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 “functionName”: *y = *y + x; For example: original y in main is equal to original value of y + x

8 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 minutes\n”, hr, min); } void hours_and_minutes(int total, int *phrs, int *pmin) *pmin = total % 60; *phrs = total / 60;

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


Download ppt "IPC144 Introduction to Programming Using C Week 6 – Lesson 1"

Similar presentations


Ads by Google