Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
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 Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
 2006 Pearson Education, Inc. All rights reserved Pointers.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 13: 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.
Chapter 8 Pointers C++ How to Program, 9/e
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
 2007 Pearson Education, Inc. All rights reserved C Pointers.
(continue) © by Pearson Education, Inc. All Rights Reserved.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
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)
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Data Types Declarations Expressions Data storage C++ Basics.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
1 Lecture 12 Pointers and Strings Section 5.4, ,
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
Variables and memory addresses
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
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++
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
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.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2006 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
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.
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Fundamentals of Characters and Strings
Student Book An Introduction
8 Pointers.
Introduction to C++ Programming
Pointers and Pointer-Based Strings
Arrays Arrays A few types Structures of related data items
Lecture 2 Arrays & Pointers May 17, 2004
C++ Programming Lecture 20 Strings
Lecture 2 Arrays & Pointers September 7, 2004
Presentation transcript:

Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect reference to data. –Pointers must be defined before they can be used.

Pointers Pointer declarations: * ; Pointer initialization: * = 0; // 0 == NULL * = ;

Address Operator The & is applied to the pointer variable to access the operand address. char let = ‘A’; char *letter; letter = &let; // The same as *letter = let

Dereferencing Dereferencing a pointer occurs when the * operator is used to access the data stored in the pointer variable. cout << *letter << endl; –The above is the same as: cout << let << endl;

Function Calls using Pointers Function calls using pointer arguments are call-by-reference. –Passing pointers to functions saves large data structures from being passed to the function. Remember call-by-reference implies that the original data can be modified.

Function Calls using Pointers The function call must send the address of the variable. functionName( & ); The function definition must use an indirection operator to create an alias for the variable. functionName ( * ) { … }

const Qualifier The const qualifier defines a value to be a constant. –Any value that is passed to a function and should not be modified by the function must be defined as const to ensure it is not modified.

const Qualifier A non-constant pointer to const data is a pointer that can be modified to point to any data item of the same data type. –The actual data it points to can not be changed. A const pointer to non-constant data always points to the same memory location. – The data it points to can be modified.

const Qualifier A const pointer to const data always points to the same memory location. –The data it points to can not be changed.

Passing Arrays When an array is passed to a function, to actual data sent is the address of the first element. void myFunction (int array[]) {…} void myFunction (int *array) {…} –Example Figure 5.15

Size of Arrays C++ provides the sizeof function that can be used to derive the number of elements in an array. –sizeof is a compile time operator and does not affect run-time execution. int numElements = sizeof arrayName / sizeof( );

Size of Arrays double arrayOfNums[20]; double numInArray = sizeof arrayOfNums / sizeof( double);

Pointer Arithmetic Pointers can be incremented, or decremented. –The increment and decrement operators can produce different results based upon the data type of the pointer. The pointer will not necessarily be incremented/decremented by 1.

Pointer Arithmetic int values[5]; int *valuePtr = v; valuePtr++; valuePtr--;

Pointer Arithmetic Pointer variables that point to the same array can be added and subtracted. –Need to ensure the result is valid.

Pointer Arithmetic A pointer can be assigned to another pointer only if they both have the same data type. –The only exception is a void pointer that can be used to represent a pointer of any data type. A void pointer has to first be cast to the proper data type before it can be assigned a pointer of that data type.

Pointer Comparison Pointer comparisons compare the memory address of the pointers. –This is only useful for arrays. You should use array notation for manipulating arrays.

Arrays of Pointers A multi-dimensional array is essentially an array of pointers. An array of strings is also an array of pointers. –The elements of the array point to the first character in the string. char *arrayName[4] = {“string 1”, “string 2”, “string 3”, “string 4”};

Arrays of Pointers Using arrays of pointers allows a program to contain elements that vary in size. –This saves memory.

Function Pointers Pointers can be used to store the address of a function in memory. –Such pointers can be passed to functions, returned from functions, stored in arrays, and assigned to other function pointers. –Example program – Figure 5.26

Function Pointers It is possible to have an array of function pointers. –This is useful for menu driven systems. –Example – Figure 5.28

Characters Characters in C++ include the ASCII code and individual characters are also represented as integers based upon the ASCII code. –Characters are surrounded by single quotes. ‘a’, ‘\n’ –A char variable represents either a single character or an array of characters.

Strings A string is a series of characters that are perceived as a single unit. –Strings are surrounded by double quotes. –Examples: “It’s Sunday, watch football!!” “x = 1 + y;”

Strings C++ represents strings as an array of characters that includes the null character (‘\0’) at the end. –The value of a string is the address of the first character.

Defining Strings Essentially strings are arrays: char name[] = “Mary”; Just like arrays, strings can also be represented as pointers: char *namePtr = “Mary”; –How many characters are stored in name[]?

Inputting Strings The cin functionality can be used to read in a string. cin >> aString; The above statement will only read characters until a space, tab, new line, or eof are encountered. –To ensure that the string does not exceed the length of the array: cin >> setw(lengthOfArray - 1) >> aString;

Inputting Strings An entire line of text may also be input using the getline function. cin.getline(arrayName, numberOfCharacters, delimiterCharacter); –getline will stop reading in characters when the delimiter character is reached, the eof character is entered, or the total number of elements has been read. –The default delimiter character is ‘\n’.

String Manipulation C++ provides a sting manipulation library. –Some of the functions are listed on page 346 – 347. –String program.