210 likes | 220 Views
This discussion group covers the fundamentals of pointers in C programming, including their declaration, indirection, and incrementing. It also discusses the need for pointers, their advantages and disadvantages, and the importance of writing separate functions.
E N D
CS1010 Discussion Group 11 Week 9 – Pointers
HELLO! Slides are at http://www.comp.nus.edu.sg/~yanhwa/
Lab 5 “Right. Since you have learned pointers already you may use pointer parameters.”
Pointers might be confusing but… Need to get used to them! :) you can do itttt
Pointers Declaration Lecture Summary • For example, declared as int*a_ptr; • a pointer variablea_ptrwhich may point to any int variable • Good practice to name a pointer with suffix _ptror _p so that you won’t forget it’s not a normal variable! (assuming “snake case” and not camel case)
Initialization of pointer Lecture Summary • 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”
Declaration and initialisation Lecture Summary 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”
Indirection / deferencing operator Lecture Summary • 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_ptris pointing at”
Changing the value at memory location Lecture Summary • 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”
What is hexadecimal? Extra info • 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? • 0 1 2 3 4 5 6 7 8 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
incrementing a pointer variable Lecture Summary • 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
Why do we need pointers? Lecture Summary • 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 *);
Lab 5 Worksheet Game Of Life
Tutorial 2 Answer is 310 0 5 . Try tracing it for yourself!
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?
Tutorial 4 Nice comment too
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));
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.
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..
Tutorial 7 – Why should we write separate functions? Trade-off! No hard-and-fast rule…