Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming Using C

Similar presentations


Presentation on theme: "Introduction to Programming Using C"— Presentation transcript:

1 Introduction to Programming Using C
Addresses and Pointers

2 Contents Returning two results Addresses Bidirectional parameters
The void type

3 Returning Two Results So far, all of our functions have returned a single result Now, suppose we want to pass a number of minutes and have a function return the number of hours and minutes In this case, we need to return two results!

4 Returning Two Results We can write the function like this
int toHM(int totalMins, int residualMins) { int hours = totalMins / 60; residualMins = totalMins % 60; return hours; } Unfortunately, this will not work since residualMins is passed down but not passed back

5 Returning Two Results The problem is that we pass residualMins by making a copy of its value and passing it Any changes to the copy are not returned to the calling function What we need to do is Pass the address of residualMins Have the function dereference the address to retrieve the value Have the function store the result at the address of residualMins

6 Addresses So, how do we work with addresses?
We can declare a variable to be a pointer to an integer, rather than an integer int *p; This says that p is a variable which points to or stores the address of an integer p is not an integer – it is just the address of an integer This is like saying that the address on an envelope is not your house, just the address of your house

7 Addresses OK, so now we can store an address
But, how do we get an address? & is the address operator that will return the address of a variable * is the dereference operator to get the value stored at an address int *p; /* pointer to an int */ int i = 9; /* a regular int */ p = &i; /* set p to point to i */ *p = 99; /* set value pointed to by p to 99 */ printf(“%d\n”, *p); /* print value pointed to by p, 99 */ * See addressdemo

8 Bidirectional Parameters
We can use addresses and pointers to pass data to a function and return data from a function Declare the parameter as a pointer Use the & operator to generate the address to pass to the function Within the function, use the * operator to access the value that is pointed to by the pointer

9 Bidirectional Parameters
We can now rewrite our function like this int toHM(int totalMins, int *residualMins) { int hours = totalMins / 60; *residualMins = totalMins % 60; return hours; }

10 Bidirectional Parameters
To call this function, we have to pass the address of the variable whose value we want returned main() { int hours, mins, totalMins; totalMins = 200; hours = toHM(totalMins, &mins); printf(“%d hours and %d minutes”, hours, mins); } Note how & is used to create the address of the parameter whose value is to be returned * See paramdemo.c

11 The void Type Our function returns
The hours as the return value The minutes as a bidirectional parameter While this works, it would make more sense to return both values via parameters So what would the function return? Nothing In C this is called void Let’s rewrite our function to return void

12 Function Using void main() { int hours, mins, totalMins;
toHM(totalMins, &hours, &mins); printf(“%d hours and %d minutes”, hours, mins); } void toHM(int totalMins, int *hours, int *residualMins) *hours = totalMins / 60; *residualMins = totalMins % 60; return;

13 Function Using void Points to note
Both hours and minutes are passed as pointers The function toHM no longer returns a value and is not assigned to a variable The dereference operator is used to access all parameters which are passed as pointers The return statement no longer returns a value


Download ppt "Introduction to Programming Using C"

Similar presentations


Ads by Google