Chapter 7 Pointers and C-Strings

Slides:



Advertisements
Similar presentations
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Advertisements

Chapter 10.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
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 CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
(continue) © by Pearson Education, Inc. All Rights Reserved.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
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.
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.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
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.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 7 Single-Dimensional Arrays and C-Strings.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
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.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Enumerated Types and Strings Types.
Pointers What is the data type of pointer variables?
Pointers and Dynamic Arrays
Chapter 3 Assignment and Interactive Input.
Chapter 9: Pointers.
Motivation and Overview
Chapter 6 Arrays Lecturer: Mrs Rohani Hassan
Pointers.
Objectives Identify the built-in data types in C++
Pointers and Pointer-Based Strings
Student Book An Introduction
CSCE 210 Data Structures and Algorithms
Lecture 6 C++ Programming
8 Pointers.
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 9: Pointers.
Chapter 7 Single-Dimensional Arrays
Chapter 2 Elementary Programming
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers and Dynamic Variables
7 Arrays.
Chapter 9: Pointers.
Pointers, Dynamic Data, and Reference Types
Pointers and Pointer-Based Strings
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 12 Pointers and Memory Management
Chapter 7 Single-Dimensional Arrays and C-Strings
7 Arrays.
C++ Programming Lecture 17 Pointers – Part I
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
Standard Version of Starting Out with C++, 4th Edition
Introducing Arrays Array is a data structure that represents a collection of the same types of data. From resourses of Y. Daniel Liang, “Introduction.
Lecture 2 Arrays & Pointers September 7, 2004
CISC181 Introduction to Computer Science Dr
Pointers, Dynamic Data, and Reference Types
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Chapter 7 Pointers and C-Strings

Objectives To describe what a pointer is (§7.1). To learn how to declare a pointer and assign a value to it (§7.2). To access elements via pointers (§7.2). To pass arguments by reference with pointers (§7.3). To understand the relationship between arrays and pointers (§7.4). To know how to access array elements using pointers (§7.5). To declare constant pointers and constant data (§7.5). To learn how to return pointers from functions (§7.6). To use the new operator to allocate persistent memory dynamically (§7.7). To test and convert characters using the character functions (§7.9.1). To store and access strings using arrays and pointers (§7.9.2). To read strings from the keyboard (§7.9.3). To process strings using the string functions (§7.9.4).

What is a Pointer? Pointer variables, simply called pointers, are designed to hold memory addresses as their values. Normally, a variable contains a specific value, e.g., an integer, a floating-point value, and a character. However, a pointer contains the memory address of a variable that in turn contains a specific value.

Declare a Pointer Like any other variables, pointers must be declared before they can be used. To declare a pointer, use the following syntax: dataType *pVarName; Each variable being declared as a pointer must be preceded by an asterisk (*). For example, the following statement declares a pointer variable named pCount that can point to an int varaible. int *pCount; TestPointer Run

Dereferencing Referencing a value through a pointer is called indirection. The syntax for referencing a value from a pointer is *pointer For example, you can increase count using count++; // direct reference or (*pCount)++; // indirect reference

Pointer Type A pointer variable is declared with a type such as int, double, etc. You have to assign the address of the variable of the same type. It is a syntax error if the type of the variable does not match the type of the pointer. For example, the following code is wrong. int area = 1; double *pArea = &area; // Wrong

Initializing Pointer Like a local variable, a local pointer is assigned an arbitrary value if you don’t initialize it. A pointer may be initialized to 0, which is a special value for a pointer to indicate that the pointer points to nothing. You should always initialize pointers to prevent errors. Dereferencing a pointer that is not initialized could cause fatal runtime error or it could accidentally modify important data.

Caution You can declare two variables on the same line. For example, the following line declares two int variables: int i = 0, j = 1; Can you declare two pointer variables on the same line as follows? int* pI, pJ; No, this line is equivalent to int *pI, pJ;

Passing Arguments by Reference with Pointers There are three ways to pass arguments to a function in C++: pass by value, pass by reference with reference arguments, and pass by reference with pointers. TestPointerArgument Run

Arrays and Pointers Recall that an array variable without a bracket and a subscript actually represents the starting address of the array. In this sense, an array variable is essentially a pointer. Suppose you declare an array of int value as follows: int list[6] = {11, 12, 13, 14, 15, 16};

Array Pointer *(list + 1) is different from *list + 1. The dereference operator (*) has precedence over +. So, *list + 1 adds 1 to the value of the first element in the array, while *(list + 1) dereference the element at address (list + 1) in the array. ArrayPointer Run PointerWithIndex Run

Using const with Pointers You learned how to declare a constant using the const keyword. A constant cannot be changed once it is declared. You can declare a constant pointer. For example, see the following code: double radius = 5; double * const pValue = &radius; ConstParameter Run

Returning Pointers from Functions You can use pointers as parameters in a function. Can you return a pointer from a function? The answer is yes. WrongReverse Run CorrectReverse Run

Case Studies: Counting the Occurrences of Each Letter Generate 100 lowercase letters randomly and assign to an array of characters. Count the occurrence of each letter in the array. CountLettersInArray Run

Characters and C-Strings Strings are used often in programming. You have used string literals. A string is a sequence of characters. There are two ways to process strings in C++. One way is to treat strings as arrays of characters. This is known as pointer-based strings or C-strings. The other way is to process strings using the string class. The string class will be introduced in §9.14. This section introduces pointer-based strings.

Character Test Functions

Case Conversion Functions CharacterFunctions Run

Storing and Accessing Strings A pointer-based string in C++ is an array of characters ending in the null terminator ('\0'), which indicates where a string terminates in memory. An array can be accessed via a pointer. So a string can also be accessed via a pointer, which points to the first character in the string. So you can declare a string variable using an array or a pointer. For example, the following two declarations are both fine: char city[7] = "Dallas"; // Option 1 char *pCity = "Dallas"; // Option 2

Pointer Syntax You can access city or pCity using the array syntax or pointer syntax. For example, cout << city[1] << endl; cout << *(city + 1) << endl; cout << pCity[1] << endl; cout << *(pCity + 1) << endl; each displays character a (the second element in the string).

Reading Strings You can read a string from the keyboard using the cin object. For example, see the following code: cout << "Enter a city: "; cin >> city; // read to array city cout << "You entered " << city << endl;

Reading Strings Using getline C++ provides the cin.getline function in the iostream header file, which reads a string into an array. The syntax of the function is: cin.getline(char array[], int size, char delimitChar) The function stops reading characters when the delimiter character is encountered or when the size - 1 number of characters are read. The last character in the array is reserved for the null terminator ('\0'). If the delimiter is encountered, it is read, but not stored in the array. The third argument delimitChar has a default value ('\n').

String Functions

String Examples Run CopyString Run CombineString Run CompareString StringConversion Run

Obtaining Substrings Often it is useful to obtain a substring from a string. But there is no such function in C++. You can develop your own function for extracting substrings. The header of the function can be specified as: char * substring(char *s, int start, int end) Substring.h TestSubstring Run

Checking Palindromes A string is a palindrome if it reads the same forward and backward. The words “mom,” “dad,” and “noon,” for example, are all palindromes. CheckPalindrome Run