Welcome to CISC220 Data Structures in C++ sakai.udel.edu Office Hours: Tues 3PM - 4PM / Thurs 1PM - 2PM TA: David.

Slides:



Advertisements
Similar presentations
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
Advertisements

1 Pointers. Variable Memory Snapshot 2 int nrate = 10; The variable is stored at specific memory address A variable is nothing more than a convenient.
Kernighan/Ritchie: Kelley/Pohl:
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Variables, Pointers, and Arrays Professor Jennifer Rexford COS 217
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Welcome to CISC220 Data Structures in C++ sakai.udel.edu Office Hours: Mon / Wed 2:30PM - 4PM TAs: Miao Tang,
Pointers CSE 2451 Rong Shi.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 2/10/2010.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
System Programming Practical Session 7 C++ Memory Handling.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Variables and memory addresses
CISC220 Fall 2009 James Atlas Dec 07: Final Exam Review.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
A Computer Science Tapestry 12.1 Pointers, Memory, Abstractions l A pointer is the a variable/value that is a memory address  Addresses like 1, 2, 3,
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
Pointer Tran, Van Hoai. Pointers and Addresses  Pointer: group of cells (2,4 cells) Variable: group of cells Pointer is also a variable  Each cell (or.
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Lecture 20-something CIS 208 Wednesday, April 27 th, 2005.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
CISC220 Spring 2010 James Atlas Lecture 04: Pointers, Functions, Memory Management, ADTs, Classes, Hardware, Software, Graphics, Networking, AI, Databases,
Lecture 5 Pointers 1. Variable, memory location, address, value
Pointers and Pointer-Based Strings
Pointers & Functions.
Dynamic Memory A whole heap of fun….
C++ Pointers and Strings
Pointers and Pointer-Based Strings
CS250 Introduction to Computer Science II
Pointers and dynamic objects
Pointers & Functions.
The Stack.
Arrays and Pointers CSE 2031 Fall May 2019.
C++ Pointers and Strings
Arrays and Pointers CSE 2031 Fall July 2019.
Pointers.
Presentation transcript:

Welcome to CISC220 Data Structures in C++ sakai.udel.edu Office Hours: Tues 3PM - 4PM / Thurs 1PM - 2PM TA: David Lopez –

Who am I?

Who are you?

What will we learn? Pointers Lists Analysis/ Big O notation Stacks Queues Trees Sorting Hashing Heaps Graphs NP-completeness

How will we learn? Class exercises (5%) Homework problems/programming (5*5%) Three programming projects (30%) Three tests (two midterm * 10%, final 20%)

What is a Data Structure?

Objectives for Today Manipulate pointers –read/write –create –dereference –assign value Call functions –by value, by reference Reading - K+W P.1-P.6

Review of pointers & memory cf. K&W, Chapter P A C++ Primer section P.5

Smith Hall Newark, DE USA

How could we move the Department of Computer and Information Sciences?

Memory Address vs. Value Stored Consider memory to be a single huge array: –Each cell of the array has an address associated with it. –Each cell also stores some value. Don’t confuse the address referring to a memory location with the value stored in that location

Pointers An address refers to a particular memory location. In other words, it points to a memory location. Pointer: A variable that contains the address of a variable. z x y Location (address) name 104

Pointers How to initialize a pointer: – can initialize to NULL (i.e. not currently pointing to anything) – & operator: get address of a variable int *x; x?y? int y = 3; x?y3 x = &y; xy3

Pointers How to get the value that is pointed to? – * “ dereference operator”: get value pointed to * x returns 3 How to change the variable pointed to? – Use dereference * operator to left of = xy5 *x = 5 ; xy3

Functions f xf(x)

Call-by-value int sum(int x, int y) { return x + y; } int main(void) { cout << sum(5,6) << endl; }

Call-by-reference int sum(const int& x, const int& y) { return x + y; } int main(void) { cout << sum(5,6) << endl; }

Group Review Break into teams of 4-5 students One person write an official answer Swap answers with a neighboring group and keep scores!

Question 1 What is output from the following code: double x = 5.5; double *px = &x; cout << *px << ‘\n’; *px = 10.0; cout << x << ‘\n’;

Question 2 What is output from the following code: double x = 5.5; double y = 10.0; double* px, py; px = &x; py = &y; cout << *px << ‘\n’ << *py << ‘\n’;

Question 3 What is output from the following code: double x = 5.5; double *px = &x; *px = 3.14; double& r = *px; r = 99.44; cout << x << ‘\n’;

Question 4 What is output from the following code: void swap(int x, int y) { int temp = x; x = y; y = temp; } int main(void) { int a = 0; int b = 5; swap(a,b); cout << a << endl; }

Question 5 Change the code to work correctly using pointers: void swap(int x, int y) { int temp = x; x = y; y = temp; } int main(void) { int a = 0; int b = 5; swap(a,b); cout << a << endl; }

Question 6 What is the value of temp after each assignment? char blocks[3] = {'A','B','C'}; char *ptr = &blocks[0]; char temp; /*1*/ temp = blocks[0]; /*2*/ temp = *(blocks + 2); /*3*/ temp = *(ptr + 1); ptr = blocks + 1; /*4*/ temp = *ptr; /*5*/ temp = *(ptr + 1);

Question 7 What is the value of temp after each assignment? char blocks[3] = {'A','B','C'}; char *ptr = blocks; char temp; /*1*/ temp = *++ptr; /*2*/ temp = ++*ptr; /*3*/ temp = *ptr++; /*4*/ temp = *ptr;

Question 8 Write code to reverse a string using only pointer arithmetic (no array accessors): int main(void) { char s[10] = "abcde"; for (int i = 0; s[i] != NULL; i++) { cout << s[i]; } revtString(s); for (int i = 0; s[i] != NULL; i++) { cout << s[i]; } cout << ‘\n’; return 0; }