Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1010 Discussion Group 11 Week 9 – Pointers.

Similar presentations


Presentation on theme: "CS1010 Discussion Group 11 Week 9 – Pointers."— Presentation transcript:

1 CS1010 Discussion Group 11 Week 9 – Pointers

2 Slides are at http://www.comp.nus.edu.sg/~yanhwa/
HELLO! Slides are at

3 Lab 5 “Right. Since you have learned pointers already you may use pointer parameters.”

4 Pointers might be confusing but…
Need to get used to them! :) you can do itttt

5 For example, declared as int *a_ptr;
Lecture Summary Pointers Declaration For example, declared as int *a_ptr; a pointer variable a_ptr which may point to any int variable Good practice to name a pointer with suffix _ptr or _p so that you won’t forget it’s not a normal variable! (assuming “snake case” and not camel case)

6 “pointer is set to the address of a”
Lecture Summary Initialization of pointer What is the correct way? *a_ptr = &a; a_ptr = a; *a_ptr = a; a_ptr = &a; “pointer is set to the address of a”

7 “declare a pointer b and initialise it with address of a”
Lecture Summary Declaration and initialisation double a; double b = &a; Is this allowed / legal? Is b a pointer? double a; double *b = &a; “declare a pointer b and initialise it with address of a”

8 “go to what a_ptr is pointing at”
Lecture Summary Indirection / deferencing operator a_ptr a Are they the same since a_ptr points to a? Why? Need to use indirection operator. *a_ptr “go to what a_ptr is pointing at”

9 “go to where a_ptr is pointing to and increment it”
Lecture Summary Changing the value at memory location a_ptr++ (*a_ptr)++ Note that compiler sees *p++ as *(p++) as ++ takes precedence over *. Always use brackets for clarity. “go to where a_ptr is pointing to and increment it”

10 What is hexadecimal? Base-16 positional numeral system.
Extra info What is hexadecimal? Base-16 positional numeral system. Normal numbers is base-10 (decimal numbers), has digits 0 to 9. Thus base-16 has digits from 0 to 15. What are the digits after 9? a b c d e f Can assume Byte addressing. An address represents a unique byte in memory or the memory space, for example ffbff627 The next byte will be ffbff628

11 incrementing a pointer variable
Lecture Summary incrementing a pointer variable a_ptr++ When incrementing a int pointer variable, it will point to the next int When incrementing a char pointer variable, it will point to the next char. ffbff62c to ffbff630 ffbff627 to ffbff628 Unit 4 Exercise #1: int takes up 4 bytes float takes up 4 bytes char takes up 1 byte double takes up 8 bytes

12 Function can modify variables out of its scope thus….
Lecture Summary Why do we need pointers? Function can modify variables out of its scope thus…. Function can “return” more than one value Pointers can lead to efficient code, but affects modularity, cohesiveness and at this point we shall value cohesion more. Use pointers only when necessary Rmr function prototype parameters! void findMaxAndAverage(int [], int, int *, double *);

13 Lab 5 Worksheet Game Of Life

14 Answer is 310 0 5 . Try tracing it for yourself!
Tutorial 2 Answer is Try tracing it for yourself!

15 Tutorial 3 – compute_surface_area_and _diagonal()

16 Tutorial 4 - Triangle incenter
The red lines are the internal angle bisectors. The Cartesian coordinates of the incenter are a weighted average of the coordinates of the three vertices, using the side lengths of the triangle opp these vertices as weights What other function do we need besides the function to get the incenter of a triangle? Why do we need to use pointers? How is this related?

17 Tutorial 4 Nice comment too

18 Another disadvantage:
Tutorial 5 If a function only returns one value as an answer, is it good to still use pointers? Usually, no. Another disadvantage: The caller would have to declare a variable, and pass the address of that variable into the new function. What if there is really no intention/need to have such a variable in the caller in the first place, as shown below? printf("The GCD is %d\n", brusco_gcd(num1, num2));

19 Tutorial 6 You should know by now…. Now, a function should perform one specific task, and not a mixture of tasks. Magic word is cohesive We should refrain from mixing a computation task and an input/output task in a function. The GCD is a computation task. Printing the answer is an output task.

20 Tutorial 6 Another reason: reusability of code DON”T REPEAT YOURSELF = DRY principle What if an application needs to compute the GCD of two numbers as part of a bigger algorithm? Compute it multiple times? Having the printf() statement in the GCD function would upset such a plan..

21 Tutorial 7 – Why should we write separate functions?
Separate the functions Combine the functions Cohesive = do one task 2 (or more) values to be returned (via the pointer parameters) are so intimately related to one another that they are considered as one group of information. For example, the triangle center coordinates Might save on unnecessary variables Efficiency (if specified that it is valued over cohesion) Trade-off! No hard-and-fast rule…


Download ppt "CS1010 Discussion Group 11 Week 9 – Pointers."

Similar presentations


Ads by Google