C++ Lecture 3 Monday, 14 July 2003. Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Advertisements

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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Pointer Arithmetic Dale Roberts, Lecturer Computer.
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.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Multiple-Subscripted Array
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
(continue) © by Pearson Education, Inc. All Rights Reserved.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
0 Chap. 5 Pointers and Arrays 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers and Functions 5.6Pointer Arrays; Pointers to Pointers.
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.
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.
 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.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Data Types Declarations Expressions Data storage C++ Basics.
POINTERS.
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.
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.
1 Lecture 12 Pointers and Strings Section 5.4, ,
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
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.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
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).
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Windows Programming Lecture 03. Pointers and Arrays.
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
ECE Application Programming
Computer Programming BCT 1113
7 Arrays.
Pointers and Pointer-Based Strings
Outline Defining and using Pointers Operations on pointers
Arrays Arrays A few types Structures of related data items
Data Structures and Algorithms Introduction to Pointers
Lecture 2 Arrays & Pointers May 17, 2004
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Lecture 2 Arrays & Pointers September 7, 2004
Characters and Strings
EECE.2160 ECE Application Programming
Presentation transcript:

C++ Lecture 3 Monday, 14 July 2003

Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l Relation between pointer and array l String, string processing functions

Arrays l Declaration of an array int c[12]; l or const int max = 12; int c[max]; l References to an array c[ ] c[0], c[1], …, c[max-1]

Array Initialization int n[5] = {1, 2, 3, 4, 5}; l or int n[ ] = {1,,2, 3, 4, 5}; l n[0], n[1], …, n[4] are valid references C.f. Fig. 4.3

Array of Characters (String) char string1[20]; char string2[ ] = "string literal"; l Individual array element is a character string2[3] = 'y'; C.f. Fig. 4.12

Passing Array l Passing an array name is equivalent to passing the address of the array int a[5]; // declare array func(a); // use function void func(int a[ ]) // define function { } C.f Fig. 4.14

Array Name is a constant Address int a[100]; a is the address of a[0], a+k is the address of a[k]. a–1, or a+100, is also a valid address; C/C++ cannot check array index out of bound error during compile or run time.

Multiple-Subscripted Arrays int a[2][5]; l A 2 by 5 array. l Initialization int a[2][5] = { {0,1,2,3,4}, {0,2,4,6,8} }; l Row major convention in C/C++

Argument Passing for Multi-Dimensional Array int a[3][5]; func(a); l or func(a+1); void func(int b[ ][5]) {... C.f Fig. 4.22

Pointers l Pointer value holds the address of a variable. l Pointers are also typed, i.e, pointer to int is considered different from pointer to double. l E.g., int *p; double *fpt;

Declaration of Pointer Variables int *p; // a pointer to int int c; // an int variable p = &c; // address of c is given to p l The value of variable p is the address of c. l The value at the address stored in p is the value of c. *p = 2; // dereferencing

Address Operator & int c, *p, a[100]; p = &c; // address of c p = a; // address of array p = &a[0]; // same as above

Dereferencing Operator * int *p, c; c = 1; p = &c; // let p point to c j = *p; // j gets 1,

Operator Precedence and Associativity ( ) [ ] left to right highest ++, --, &, * right to left unary * / % left to right multiply +, - left to right additive > left to right insertion, >= left to right relational ==, != left to right equality, left to right comma

Left to Right Association l a + b + c means (a+b) + c l Precedence l a + b*c means a + (b*c)

Adding Parentheses ( … ) to show the order of evaluation l b += x = y - k*a[ j ] && 3 | u < v--; l See Appendix A, page 1214 for the complete "Operator Precedence Chart".

Answer b += (x = ((y - (k*(a[ j ]))) && (3 | (u < (v--)))));

Call-by-Reference with Pointer l Passing a pointer can modify the value in calling program. int c; cube_it(&c); void cube_it(int *p) {... C.f. Fig.5.7

Pointer to const data and const Pointers void f(const int *p); l The data pointed by p cannot be modified. int * const p = &x; // p always // pointing to x const int *const p = &x; // p always // points to x, x cannot change

Relationship between Pointers and Arrays l An expression of *(a + k) l is considered totally equivalent to a[k]. l E.g., a[0] is the same as *a.

Multi-Dimensional Array l In the declaration int a[3][7]; we can view a as pointer to array of a[0], a[1], a[2], which themselves are pointers to int array a[i][j]. l **a is the same as a[0][0].

Pointer of Pointers a a[0] a[1] a[2] a[0][0] a[0][1] a[0][2]... a[1][0] a[1][1]... a[2][0] a[2][1]

Arrays of Pointers char *suit[4] = { "Hearts", "Diamonds", "Clubs", "Spade"}; l suit[0][0] refers to 'H', l suit[3][4] refers to 'e' in "Spade", l suit[2][20] is an illegal access.

Function Pointer void (*f)(int); l f is a pointer to a function of the form void g(int). l Function names are like address, e.g, f = g; (*f)(5); // call the function

Fundamentals of Characters and Strings char c, s[10]; // declare variables c = 'a'; // can be used as small int s = "John Q"; // don't work, why? l You must do s[0] = 'J'; s[1] = 'o'; s[2] = 'h'; // etc l Or use string copy function

String Initialization char color[ ] = "blue"; char *colorPtr = "blue"; char color[ ] = {'b', 'l', 'u', 'e', '\0'}; l What is the difference between the array color and the pointer colorPtr?

Read a String char word[20]; cin >> word; // cause problem cin >> setw(20) >> word; // OK l Read an entire line char sentence[80]; cin.getline(sentence, 80);

String Processing Functions char *strcpy(char *t, const char *s); char *strncpy(char *t, const char *s, size_t n); char *strcat(char *t, const char *s); int strcmp(const char *s, const char *t); size_t strlen(const char *s);

String Processing l Examples from Fig.5.30 and C.f. Fig 5.30 and 5.31

Exercise 4.29 l (The Sieve of Eratosthenes) A prime integer is any integer that is evenly divisible only by itself and 1. Use the sieve method to find prime. Initialize a[1000] all to 1 (for true). Starting from 2, 3, …, k,..., set array indices which are all multiple of k to 0, print the nonzero indices.