Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jesús Borrego 1.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
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.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Pointers Applications
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
5. Arrays, Pointers and Strings 7 th September IIT Kanpur C Course, Programming club, Fall
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Review 1 List Data Structure List operations List Implementation Array Linked List.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Scis.regis.edu ● CS-361: Control Structures Week 2 Dr. Jesús Borrego Lead Faculty, COS Regis University 1.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Engineering Computing I Chapter 5 Pointers and Arrays.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
CSE 251 Dr. Charles B. Owen Programming in C1 Intro to Arrays Storing List of Data.
Pointer Data Type and Pointer Variables III CS1201: Programming Language 2 By: Nouf Aljaffan Edited by : Nouf Almunyif.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Pointers and Arrays Dynamic Variables and Arrays.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Windows Programming Lecture 03. Pointers and Arrays.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Chapter 7: User-Defined Functions II
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointer Data Type and Pointer Variables II
14th September IIT Kanpur
CSC 253 Lecture 8.
Chapter 10: Pointers 1.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
CSC 253 Lecture 8.
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointer Data Type and Pointer Variables III
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
C Programming Lecture-8 Pointers and Memory Management
Presentation transcript:

scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jesús Borrego 1

Topics Dynamic Data ▫Pointers ▫Address Pointers to different data types Sample programs 2

Pointer Data Type/Pointer Variables The type is integer The Name of the integer is A The address is The value stored in A is 325 int A = 325;10280 A 3 325

More Pointers 4

Running the program 5

6

PointerAndAddress 7

Declaring Pointer Variables Syntax: Examples: int *p; char *ch; These statements are equivalent: int *p; 8

Pointer Intro Program (I) 9

Pointer Intro Program (II) 10

Running the program 11

Pointer Intro Program (I) 12

Pointer Intro Program (II) 13

Pointer Intro Program (II) 14

MoreStruct 15

Running the program 16

MoreBeatles (I) 17

MoreBeatles (II) 18

MoreBeatles (III) 19

Running the program 20

PointerSwap 21

Running the Program 22

What happened? The swap did not work? The before and after are the same Need to make sure we get the values back to the calling program It is calling by Value How do we fix it? 23

Address of Operator ( & ) The ampersand, &, is called the address of operator The address of operator is a unary operator that returns the address of its operand 24

PointerSwap2 25

PointerSwap3 26

Running the program (I) 27

Running the program (II) 28

Dynamic Arrays Dynamic array: array created during the execution of a program Example: int *p; p = new int[10]; *p = 25; p++; //to point to next array component *p = 35; 29 stores 25 into the first memory location stores 35 into the second memory location

Dynamic Arrays (cont'd.) C++ allows us to use array notation to access these memory locations The statements: p[0] = 25; p[1] = 35; store 25 and 35 into the first and second array components, respectively 30

Dynamic Arrays (cont'd.) 31

Dynamic Arrays (cont'd.) 32

Dynamic Arrays (cont'd.) The value of list ( 1000 ) is constant – Cannot be altered during program execution – The increment and decrement operations cannot be applied to list If p is a pointer variable of type int, then: p = list; copies the value of list, the base address of the array, into p – We can perform ++ and -- operations on p An array name is a constant pointer 33

34 Dynamic Arrays (cont'd.)

Functions and Pointers A pointer variable can be passed as a parameter either by value or by reference To make a pointer a reference parameter in a function heading, use & : void pointerParameters(int* &p, double *q) {... } 35

Pointers and Function Return Values A function can return a value of type pointer: int* testExp(...) {... } 36

Dynamic Two-Dimensional Arrays You can create dynamic multidimensional arrays Examples: 37 declares board to be an array of four pointers wherein each pointer is of type int creates the rows of board declares board to be a pointer to a pointer

PointerArray 38

PointerArray (Cont’d) 39

Running the program 40

PrintArray2D 41

PrintArray2D (Cont’d) 42

PrintArray (Cont’d) 43

Running the Program 44

Resources Can search the internet for tutorials on pointers On YouTube: eghttp:// eg From Google, search for ▫“C++ pointer tutorial“ 45