CS250 Introduction to Computer Science II

Slides:



Advertisements
Similar presentations
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
Advertisements

Chapter 10.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
CS150 Introduction to Computer Science 1
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
(continue) © by Pearson Education, Inc. All Rights Reserved.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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.
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.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
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.
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.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 2/2/05CS250 Introduction to Computer Science II Pointers.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 8 Arrays, Strings and Pointers
What Actions Do We Have Part 1
EGR 2261 Unit 10 Two-dimensional Arrays
Pointers & Arrays.
COSC 220 Computer Science II
Pointers and Pointer-Based Strings
CSC113: Computer Programming (Theory = 03, Lab = 01)
Student Book An Introduction
Lecture 6 C++ Programming
8 Pointers.
Object-Oriented Programming Using C++
Introduction to the C Language
Object Oriented Programming COP3330 / CGS5409
CS 2308 Exam I Review.
7 Arrays.
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Arrays Kingdom of Saudi Arabia
CS150 Introduction to Computer Science 1
String What it is Why it’s useful
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
CS150 Introduction to Computer Science 1
7 Arrays.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
Arithmetic Operations
do/while Selection Structure
Data Structures and Algorithms Introduction to Pointers
Pointers & Arrays.
Reading from and Writing to Files
CS150 Introduction to Computer Science 1
Chapter 9: Pointers and String
CS150 Introduction to Computer Science 1
C++ Programming Lecture 18 Pointers – Part II
C++ Programming Lecture 20 Strings
Lecture 2 Arrays & Pointers September 7, 2004
CISC181 Introduction to Computer Science Dr
Reading from and Writing to Files Part 2
Variables and Constants
Reading from and Writing to Files
SPL – PS2 C++ Memory Handling.
Presentation transcript:

CS250 Introduction to Computer Science II Pointers and Strings 2/7/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Strings A string can be declared as: char pet[] = “cat”; char *pPet = “dog”; In the second declaration above, pPet points to the first letter (d) of the string on the right Depending on the compiler, the space in memory that contains dog may or may not be modifiable If a string literal is to be modified, it should always be stored in a character array 2/7/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Strings Assuming that the string pet has been declared as: char pet[] = “cat”; Write a function that will output the contents of the string. The function should accept the array and its size Write a function that will output the contents of the string. The function should accept a pointer to char 2/7/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Strings and Pointers Write a function strLength that accepts a string (as a pointer) and returns the length of the string 2/7/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Strings and Pointers int strLength (const char *pStr) { int index; for (index = 0; *(pStr + index) != '\0'; index ++); return index; } What is the purpose of const in the function header? Why is the null character \0 in single quotes? Is the ; at the end of the for loop a mistake? What would happen if the ; was eliminated? 2/7/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Pointers and Strings int strLength1 (const char *pStr) { int index; for (index = 0; *(pStr++) != '\0'; index ++); return index; } Will the above function give the same output as the function on the previous slide? 2/7/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Pointer Arithmetic int strLength2 (char *pStr) { char *pTemp = pStr; while (*pTemp) pTemp ++; return pTemp - pStr; } 2/7/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II What is happening? int sumInts (int *pArray, int size) { int sum = 0; int index; for (index = 0; index < size; index ++) sum += * pArray ++; return sum; } int array[] = {10, 20, 30, 40, 50}; creates an array as follows: Address Value Element 2000 10 0 2004 20 1 2008 30 2 2012 40 3 2016 50 4 2/7/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Constant Pointers So far we have seen: Nonconstant pointers to nonconstant data Nonconstant pointers to constant data What about constant pointers? We said that array names are constant pointers to the first element in the array. What does that mean? 2/7/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Constant Pointers int * const pNum, num, num2; num = 9; num2 = num + 8; pNum = &num; *pNum *= 2; pNum = &num2; // ERROR pNum has been declared as a constant pointer It cannot point to any other memory location 2/7/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Bubble Sort 3 1 4 7 2 2 1 4 7 3 2 1 4 7 3 2 1 4 7 3 2 1 4 7 3 2 1 7 4 3 2 1 7 4 3 2 7 1 4 3 First pass 2 7 1 4 3 2 7 1 4 3 2 7 1 4 3 2 7 1 4 3 2 7 1 4 3 2 7 4 1 3 2 7 4 1 3 2 7 4 1 3 Second pass Continue while cells are being swapped……. 2/7/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Bubble Sort Let’s look through the code that will perform the bubble sort described on the previous slide http://zeus.cs.pacificu.edu/shereen/cs250/lectures/04bubble.html 2/7/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Arrays of Pointers What do you make of the following declaration? char *cardSuits[4] = {"Clubs", "Diamonds", "Hearts", "Spades"}; What gets output in each of the following cases? cout << cardSuits[1] << endl; cout << *cardSuits[1] << endl; 2/7/05 CS250 Introduction to Computer Science II

CS250 Introduction to Computer Science II Summary Today I introduced Pointers and strings Constant pointes Bubble sort Arrays of pointers We have covered: All of chapter 5 2/7/05 CS250 Introduction to Computer Science II