1 2/2/05CS250 Introduction to Computer Science II Pointers.

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
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.
1 Pointers & functions Pointers allow us to simulate pass by reference. void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main () { int.
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 and References (1) THE REFERENCE OPERATOR REFERENCES POINTERS THE DEREFERENCE OPERATOR DERIVED TYPES RETURNING A REFERENCE.
Topic 8 – Introduction to Pointers and Function Output Parameters.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
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.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
[S. Uludag] CIS / CSC 175 Problem Solving and Programming I Winter 2010 Suleyman Uludag Department of Computer Science, Engineering and Physics (CSEP)
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.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
POINTERS.
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.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
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.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define,
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
CSci 162 Lecture 6 Martin van Bommel. Functions on Structures Rather than pass a copy of structure to a function, or return a copy back as the function.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Chapter 7 Pointers and C-Strings
Pointers Introduction
Pointers and Pointer-Based Strings
Student Book An Introduction
Pointers.
Pointer.
Pointers Psst… over there.
Pointer Data Type and Pointer Variables
Pointers Psst… over there.
Pointer Basics Psst… over there.
Chapter 9: Pointers.
Chapter 9: Pointers.
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
5.1 Introduction Pointers Powerful, but difficult to master
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
C++ Programming Lecture 17 Pointers – Part I
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
Arithmetic Operations
Data Structures and Algorithms Introduction to Pointers
CS250 Introduction to Computer Science II
CS150 Introduction to Computer Science 1
Pointers and dynamic objects
Pointer Data Type and Pointer Variables
Pointer Basics Psst… over there.
Pointer Data Type and Pointer Variables
CISC181 Introduction to Computer Science Dr
CS150 Introduction to Computer Science 1
Presentation transcript:

1 2/2/05CS250 Introduction to Computer Science II Pointers

2 2/2/05CS250 Introduction to Computer Science II Pointers  Pointers are one of the most powerful features of C++  Pointers give programmers more control over the computer’s memory  A pointer is the memory address of a variable  How are variables declared in C++?

3 2/2/05CS250 Introduction to Computer Science II Pointer Declarations  The memory address of a variable can be stored in another variable called a pointer  Pointers are declared using the * operator  The following declares a pointer to an integer o int *pX;  In the following statement, x is an integer and pX is a pointer to an integer o int *pX, x;

4 2/2/05CS250 Introduction to Computer Science II Pointer Declarations  How would you create two pointers to doubles?  Notes: o When naming pointer variables, the book uses the convention of having ptr at the end of the name (eg. countPtr, xPtr) o Using our coding standards, we will use the convention that all pointer variables start with a small p (eg. pCount, pX)  Coding standards are available at ds.html ds.html

5 2/2/05CS250 Introduction to Computer Science II Address Operator  How do we assign to a pointer the address of a variable?  Use the address operator (&)  & returns the memory address of it’s operand  Example: o pX = &x;  Where have we used & before?

6 2/2/05CS250 Introduction to Computer Science II Address Operator  Operand of the address operator must be an lvalue  An lvalue is something to which a value can be assigned  Address operator cannot be applied to constants o int const NUM = 98; o pX = # // ERROR o pX = &8; // ERROR

7 2/2/05CS250 Introduction to Computer Science II Pointer Operations int x, *pX; x = 8; // set x to a value of 8 pX = &x; // set the pointer variable to point // to the address of x cout << "x is: " << x << endl; cout << "Address of x is: " << pX << endl; cout << "Address of x is: " << &x << endl;

8 2/2/05CS250 Introduction to Computer Science II Indirection Operator  How can we use the pointer variable to modify the value in the variable? o i.e. how to use pX to change the value of x  Answer: use the indirection operator ( * )  The * operator returns a synonym to whatever the pointer variable is pointing to  Using the example on the previous slide o cout << "pX is pointing to: " << *pX << endl;

9 2/2/05CS250 Introduction to Computer Science II Indirection Operator  Using * as we did in the previous example is called dereferencing the pointer  Using our example, how can we dereference pX so that it changes the value of x from 8 to 10?  How can we change the value of x to a value entered by the user using the indirection operator?

10 2/2/05CS250 Introduction to Computer Science II Common Pointer Mistakes  What is wrong with the following? int x, *pX; x = 8; *pX = 2; pX = 9; *x = 4;

11 2/2/05CS250 Introduction to Computer Science II Pointers and Functions  What are the two ways of passing arguments into functions?  Write two functions square1 and square2 that will calculate the square of an integer. o square1 should accept the argument passed by value, o square2 should accept the argument passed by reference.

12 2/2/05CS250 Introduction to Computer Science II Pointers and Functions  There is a third way of passing arguments into functions  It’s called passing by reference without using reference arguments  The address of the argument is passed instead of the argument itself

13 2/2/05CS250 Introduction to Computer Science II Passing by reference void square3 (int *pNum) { *pNum *= *pNum; }  What would a function call to the above function look like?

14 2/2/05CS250 Introduction to Computer Science II Function Call intval = 5; square3( &intval ); cout << intval << endl;

15 2/2/05CS250 Introduction to Computer Science II Summary  Today I introduced o The concept of pointer variables o The address operator o The indirection operator  We have covered: o P