1 / 21

CS1010 Discussion Group 11

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.

ldugan
Download Presentation

CS1010 Discussion Group 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1010 Discussion Group 11 Week 9 – Pointers

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

  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. 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)

  6. 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”

  7. 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”

  8. 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”

  9. 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”

  10. 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

  11. 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

  12. 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 *);

  13. Lab 5 Worksheet Game Of Life

  14. Tutorial 2 Answer is 310 0 5 . 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. 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? Trade-off! No hard-and-fast rule…

More Related