CS1010 Discussion Group 11 Week 9 – Pointers.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

WEEK 5 Class Activities Lecturer’s slides.
Programming and Data Structure
Pointers Ethan Cerami Fundamentals of Computer New York University.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
UNIT 14 Functions with Pointer Parameters.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
Review 1 List Data Structure List operations List Implementation Array Linked List.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Pointers *, &, array similarities, functions, sizeof.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
UNIT 8 Pointers.
CS1010: Programming Methodology
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers and Reference parameters.
Windows Programming Lecture 03. Pointers and Arrays.
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
CS1010 Programming Methodology
CS1010 Programming Methodology
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
User-Written Functions
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 8 Arrays, Strings and Pointers
Chapter 7: User-Defined Functions II
Variables Mr. Crone.
UNIT 5 C Pointers.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
CS1010 Discussion Group 11 Week 4 – Overview of C programming.
CS1010 Programming Methodology
CS1010 Discussion Group 11 Week 6 – One dimensional arrays.
CS1010 Discussion Group 11 Week 7 – Two dimensional arrays.
Pointers.
CS1010 Discussion Group 11 Week 12 – Structured Structures.
Pointers.
Arrays in C.
User-Defined Functions
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
void Pointers Lesson xx
Pointers.
Lesson #6 Modular Programming and Functions.
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Lecture 11 C Parameters Richard Gesick.
Bases and Representations, Memory, Pointers, Arrays, For-Loops
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Writing Functions.
Pointers.
CS150 Introduction to Computer Science 1
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Lesson #6 Modular Programming and Functions.
Building Java Programs
Building Java Programs
Building Java Programs
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
EECE.2160 ECE Application Programming
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
EECE.2160 ECE Application Programming
Building Java Programs
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
ICS103: Programming in C 6: Pointers and Modular Programming
Introduction to Pointers
Presentation transcript:

CS1010 Discussion Group 11 Week 9 – Pointers

Slides are at http://www.comp.nus.edu.sg/~yanhwa/ 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

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)

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

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

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

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

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? 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 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

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

Lab 5 Worksheet Game Of Life

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

Tutorial 3 – compute_surface_area_and _diagonal()

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

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

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