Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 PointersIntroduction Dale Roberts, Lecturer Computer Science,

Slides:



Advertisements
Similar presentations
Data Structures Using C++ 2E
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Pointer Arithmetic Dale Roberts, Lecturer Computer.
BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CS 141 Computer Programming 1 1 Pointers. Pointer Variable Declarations and Initialization Pointer variables –Contain memory addresses as values –Normally,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 (Pointers) Lecture 1 –Follow class notes –Some.
Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
CS1061 C Programming Lecture 13: Pointers A. O’Riordan, 2004.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
[S. Uludag] CIS / CSC 175 Problem Solving and Programming I Winter 2010 Suleyman Uludag Department of Computer Science, Engineering and Physics (CSEP)
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5: Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Dale Roberts, Lecturer Computer Science, IUPUI.
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.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Lecture 17: The Last Puzzle Piece with Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Lecture 9 - Pointers 1. Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference Pointer.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Pointers Introduction CSCI 230
Week 9 - Pointers.
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 7 - Pointers Outline 7.1 Introduction
POINTERS.
Abstract Data Types Polynomials CSCI 240
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
Variable Declarations, Data types, Expressions
Variable Declarations, Data types, Expressions
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
Pointers and Pointer-Based Strings
Pointers Call-by-Reference CSCI 230
Pointers Kingdom of Saudi Arabia
Introduction to Problem Solving and Programming
5.1 Introduction Pointers Powerful, but difficult to master
Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.
EENG212 ALGORITHMS & DATA STRUCTURES
C++ Programming Lecture 17 Pointers – Part I
Characters and Strings
CISC181 Introduction to Computer Science Dr
Pointers and pointer applications
C Pointers Another ref:
Presentation transcript:

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 PointersIntroduction Dale Roberts, Lecturer Computer Science, IUPUI

Dale Roberts What is Pointer Bring your money to IUPUI SL-280 to exchange hostage “Find Instruction Under The crabapple tree next to old law school ” “Find next message on the top of traffic light on Michigan St. and West St. Phone rings and says “Deliver ransom to I-65 and West St. intersection Hostage Location A: Highway Intersection Location B: Traffic Light Location C: Crabapple Tree Location D: IUPUI SL-280 CS Dept. CSABCD Address of next location A pointer is an address.

Dale Roberts What is Pointer Pig Pen points to Lucy; Lucy points to Sally; Sally points to Linus; Linus points to Charlie; Charlie points to Snoopy; Snoopy has Woodstock Example: Find out who is with Woodstock among peanut gang ? Snoopy CharlieLinus … Pig Pen “ Lucy knows ” Lucy “Sally knows” Sally “ Linus knows ” Linus “ Charlie knows ” Charlie “ Snoopy knows ” Woodstock Woodstock is with Snoopy Snoopy An instance uses a pointer to link to a next instance making a chain.

Dale Roberts Pointer declarations: datatype snoopy, *charlie, **linus; snoopy = ; /* snoopy’s content is Woodstock */ charlie = &snoopy; /* charlie’s content is the info (pointer) to locate snoopy, which is snoopy’s address */ linus = &charlie; /* linus’s content is the info (pointer) to locate charlie, which is charlie’s address*/ In general, we can rewrite charlie using variable of snoopyPtr (Snoopy’s pointer) and rewrite linus using variable of snoopyPtrPtr (pointer to Snoopy’s pointer); Note that this is only a naming convention. The only requirement is that the variable be a valid identifier: begin with a letter followed by letters, digits or _.

Dale Roberts Pointer Variable Declarations and Initialization PointersDefinition: A pointer is a variable that contains address of another variable. A powerful feature of C, but difficult to master Pointers enable programs to simulate call-by- reference and create/manipulate dynamic data structures Close relationship with arrays and strings

Dale Roberts Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain address of a variable that has a specific value (indirect reference) Indirection – referencing a pointer value count 7 7 countPtr

Dale Roberts Pointer Declarations type *variable_name * used with pointer variables * used with pointer variables Example: int *myPtr; Declares a pointer to an int (pointer of type int * ) Multiple pointers require using a * before each variable declaration Can declare pointers to any data type Example: int *myPtr1, *myPtr2; float *pq; char *pc; Pointer Variable Declarations

Dale Roberts Pointer Variable Initialization Initialize pointers to 0, NULL, or an address 0 or NULL – points to nothing ( NULL preferred) Example: int *ptr, x; x=10; ptr = &x; 5000 ptr FFFF x 5000 ptr x 10 FFFF 5000 ptr FFFF x 10

Dale Roberts Pointer Operators & : Address Operator Returns address of operand int y = 5; int *yPtr; yPtr = &y; /* yPtr “points to” y */ /* yPtr gets address of y */ yptr y 5 yPtr y 5 address of y is the value of yPtr

Dale Roberts Pointer Operators * : Indirection / De-referencing Operator Returns a synonym/alias of what its operand points to *yptr returns y (because yptr points to y ) *yptr returns y (because yptr points to y ) * can be used for assignment that returns alias to an object * can be used for assignment that returns alias to an object *yptr = 7; // changes y to 7 Dereferenced pointer (operand of * ) must be a variable (no constants) * and & are inverses They cancel each other out

Dale Roberts Pointer Operators Example: int i = 5; int *pi; pi = &i; /* place the address of i into pi */ Assume Symbol Table i.e. &i = 874, i = 5; &pi = 902, pi = 874; *pi = ? *pi = 5; // same as i = 5; *pi = *pi * 2; // same as i = i * 2; *pi *= 2; // same as i *= 2; pii 874 variableaddressvalue at the address i 8745 pi

Dale Roberts Example: int i,*pi,**ppi; i = 5; pi = &i; ppi = π ppipii VariableAddressValue at address i1005 pi ppi UsageMeaningValue pi address of int 100 *piint value 5 &pi address of pointer 104 ppi address of pointer 104 *ppi address of int 100 **ppiint value 5