Pointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as

Similar documents
Fundamentals of Programming Session 20

Programming for Engineers Pointers

C++ Programming Chapter 7 Pointers

Lecture 05 POINTERS 1

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Homework #3 CS2255 Fall 2012

pointers + memory double x; string a; int x; main overhead int y; main overhead

What is an algorithm?

BITG 1113: POINTER LECTURE 12

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Pointers, Dynamic Data, and Reference Types

Lecture 23: Pointer Arithmetic

Functions. Introduction :

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Relationship between Pointers and Arrays

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

Exam 3 Chapters 7 & 9

C Pointers. 6th April 2017 Giulio Picierro

Pointers. 10/5/07 Pointers 1

C++ for Java Programmers

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Chapter 9: Pointers. Copyright 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.

Chapter 9: Getting the Address of a Variable. Something Like Pointers: Arrays. Pointer Variables 8/23/2014. Getting the Address of a Variable

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CMPS 221 Sample Final

Week 3: Pointers (Part 2)

Tema 6: Dynamic memory

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana

1. In C++, reserved words are the same as predefined identifiers. a. True

Fundamentals of Programming Session 19

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Constants, References

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Pointer Data Type and Pointer Variables

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1

C++/Java. C++: Hello World. Java : Hello World. Expression Statement. Compound Statement. Declaration Statement

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

FORM 2 (Please put your name and form # on the scantron!!!!)

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

MYcsvtu Notes LECTURE 34. POINTERS

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

Chapter 6 - Pointers

... Lecture 12. Pointers

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Array Elements as Function Parameters

CS 103 Unit 13 Slides

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Arrays. Week 4. Assylbek Jumagaliyev

CS2255 HOMEWORK #1 Fall 2012

Pointers. Variable Declaration. Chapter 10

Pointers, Arrays and C-Strings

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Separate Compilation Model

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

1 Pointer Concepts. 1.1 Pointer Examples

Integer Data Types. Data Type. Data Types. int, short int, long int

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

Chapter 5 - Pointers and Strings

Chapter 5 - Pointers and Strings

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

CS132 Algorithm. Instructor: Jialiang Lu Office: Information Center 703

Review Questions II KEY

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

CpSc 1011 Lab 11 Pointers

Pointers II. Class 31

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

LECTURE 3. Compiler Phases


Chapter 9. Pointers and Dynamic Arrays

CSCE Practice Midterm. Data Types

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

Lecture 9 - Pointers 1

Topics: Material through example 19 (types, operators, expressions, functions, selection, loops, arrays)

True or False (12 Points)

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

CSCE Practice Midterm. Data Types

CS201- Introduction to Programming Current Quizzes

Enumerated Types. Mr. Dave Clausen La Cañada High School

Functions, Arrays & Structs

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Introduction to Computer Science Midterm 3 Fall, Points

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

LAB 4.1 Relational Operators and the if Statement

C++ Final Exam 2017/2018

Unit 14. Passing Arrays & C++ Strings

ComS 228 Exam 1. September 27, 2004

Dynamic Allocation of Memory

Transcription:

Pointers and Strings Chapters 10, 12 2/5/07 CS250 Introduction to Computer Science II 1 3.1 Pointers and Arrays (10.3) An array of ints can be declared as o int numbers[] = 1, 2, 3, 4, 5; numbers is also a pointer to the first element in the array Therefore, it can be dereferenced to access the elements of the array o *numbers = 2; o What are the contents of the above array now? 2/5/07 CS250 Introduction to Computer Science II 2 3.2 Pointers and Arrays (10.3) The name of the array is a pointer to the first element in the array What about the other elements in the array? o You can add 1 to the array name to access the second element o You can add 2 to the array name to access the second element.and so on When adding a number to the array name, you are actually adding that number times the size of the element in the array 2/5/07 CS250 Introduction to Computer Science II 3 1

3.3 Pointers and Arrays (10.3) int numbers[] = 1, 2, 3, 4, 5; *(numbers + 1) = 1; *(numbers + 2) = 1; *(numbers + 3) = 1; *(numbers + 4) = 1; What are the contents of the array now? What would happen if we did the following: o *(numbers + 5) = 1; 2/5/07 CS250 Introduction to Computer Science II 4 3.4 Pointers and Arrays (10.3) Rewrite the following so that it uses pointer notation instead of subscript notation for(int x = 0; x < 100; x++) cout << array[x] << endl; 2/5/07 CS250 Introduction to Computer Science II 5 3.5 Strings What is a string in C++? How have we declared string variables? We have used two ways. 2/5/07 CS250 Introduction to Computer Science II 6 2

3.6 C-Strings (12.1) In C++, strings are arrays of characters that end in the null character \0 A C-string can be declared as: o char pet[] = cat ; o char *ppet = cat ; 2/5/07 CS250 Introduction to Computer Science II 7 3.7 Strings and Pointers When declaring an array, the name of the array is also a constant pointer to the first element in the array int array[] = 2, 4, 6, 8; int *parray; parray = array; parray = &array[0]; cout << array[2] << *(parray + 2); parray ++; array ++; // ERROR 2/5/07 CS250 Introduction to Computer Science II 8 3.8 Strings Assuming that the string pet has been declared as: o 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/5/07 CS250 Introduction to Computer Science II 9 3

3.9 Strings and Pointers Write a function strlength that accepts a string (as a pointer) and returns the length of the string 2/5/07 CS250 Introduction to Computer Science II 10 3.10 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? Is the ; at the end of the for loop a mistake? What would happen if the ; was eliminated? 2/5/07 CS250 Introduction to Computer Science II 11 3.11 Pointer Arithmetic (10.4) int strlength2 (char *pstr) char *ptemp = pstr; while (*ptemp) ptemp ++; return ptemp - pstr; 2/5/07 CS250 Introduction to Computer Science II 12 4

3.12 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/5/07 CS250 Introduction to Computer Science II 13 3.13 Constant Pointers So far we have seen: o Nonconstant pointers to nonconstant data o 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/5/07 CS250 Introduction to Computer Science II 14 3.14 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/5/07 CS250 Introduction to Computer Science II 15 5

3.15 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/5/07 CS250 Introduction to Computer Science II 16 Summary Today I introduced o The relationship between pointers and arrays o Pointers and strings o Pointer arithmetic o C-strings o Constant pointers o Arrays of pointers We have covered: o 10.3, 10.4, 12.1 2/5/07 CS250 Introduction to Computer Science II 17 6