Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.

Similar documents
Pointers and Strings. Adhi Harmoko S, M.Komp

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

C Pointers. 7.2 Pointer Variable Definitions and Initialization

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

Chapter 6 - Pointers

Lecture 05 POINTERS 1

Chapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

C Pointers Pearson Education, Inc. All rights reserved.

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

Chapter 5 - Pointers and Strings

Chapter 5 - Pointers and Strings

Pointers. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Lecture 4. Default Arguments. Set defaults in function prototype. int myfunction( int x = 1, int y = 2, int z = 3 );

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

MYcsvtu Notes LECTURE 34. POINTERS

IS 0020 Program Design and Software Tools

CSC 211 Intermediate Programming. Pointers

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

BITG 1113: POINTER LECTURE 12

C++ Programming Chapter 7 Pointers

Functions and Recursion

Pointers, Arrays and C-Strings

Functions. Function Prototypes. Function prototype is needed if the function call comes before the function definition in the program.

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

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):

C++ Programming Lecture 11 Functions Part I

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Homework #3 CS2255 Fall 2012

Pointers, Dynamic Data, and Reference Types

C Functions. Object created and destroyed within its block auto: default for local variables

Introduction to C++ Systems Programming

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

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

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

Chapter 18 - C++ Operator Overloading

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Exam 3 Chapters 7 & 9

C++ for Java Programmers

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

CS201- Introduction to Programming Current Quizzes

CS242 COMPUTER PROGRAMMING

Programming for Engineers Pointers

POINTERS. Content. Pointers. Benefits of Pointers. In this chapter, you will learn:

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

[0569] p 0318 garbage

Operator Overloading in C++ Systems Programming

IS 0020 Program Design and Software Tools

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.

Chapter 15 - C++ As A "Better C"

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

CSC 330 Object-Oriented Programming. Exception Handling CSC 330

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Exception Handling Pearson Education, Inc. All rights reserved.

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

Pointers in C. Recap: Anatomy of a Typical C Program. declarations variables functions. int main (void){ declarations; statements; return value; }

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Pointers II. Class 31

Introduction to Programming

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

CHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

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

12. Pointers Address-of operator (&)

In this chapter, you will learn about: Pointers. Dynamic Arrays. Introduction Computer Science 1 CS 23021

Introduction to C++ Introduction to C++ 1

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

PROGRAMMAZIONE I A.A. 2017/2018

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

Tokens, Expressions and Control Structures

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Arrays. Week 4. Assylbek Jumagaliyev

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

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

Exercise 1.1 Hello world

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 6: Pointers

Tutorial 10 Pointers in C. Shuyue Hu

Pointers. 10/5/07 Pointers 1

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

Chapter 7 C Pointers

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

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

Fundamentals of Programming Session 19

Introduction. W8.2 Operator Overloading

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

W8.2 Operator Overloading

Constants, References

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

a data type is Types

Pointers. Variable Declaration. Chapter 10

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

Lecture 9 - Pointers 1

Transcription:

Outline Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples. 1

Introduction A pointer is a variable that contains a memory address Pointers Powerful, but difficult to master. Simulate call-by-reference. Allow the creation of dynamic data structures that shrink and grow in size during run time, such as stacks, link lists, etc. Close relationship with arrays and strings. 2

Pointer Variable Declarations and Initialization I Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain the address of a variable that has a specific value (indirect reference) Indirection (the act of using a pointer) Referencing a pointer value (accessing the contents of the memory location indicated by the address found inside the pointer). Pointer declarations * indicates that a variable is a pointer int *myptr; // this statement means create a pointer that points to an integer data declares a pointer to an int, a pointer of type int * Multiple pointers require multiple asterisks int *myptr1, *myptr2; 3

Pointer Variable Declarations and Initialization II Can declare pointers to any data type Pointers initialization Initialized to 0, NULL, or an address 0 or NULL points to nothing Suppose that you defined x as float x = 3.5; You have two options to declare a pointer that points to x: 1- float *pt = &x; (now pt is a pointer to x) 2- float *pt; pt = &x; (now pt is a pointer to x) 4

Pointer Operators I & (address operator) Returns the address of its operand It is different from the reference operator used in declaring reference variables. Can be applied to variables only (cannot be applied to expressions or constants syntax error) Example int y = 5; int *yptr; yptr = &y; yptr points to y // yptr gets address of y yptr y 5 yptr 500000 600000 y 600000 5 address of y is value of yptr 5

Pointer Operators II * (indirection/dereferencing operator) Returns the value of what its operand points to *yptr returns y (because yptr points to y). * can be used to assign a value to a location in memory *yptr = 7; // changes y to 7 Dereferenced pointer (operand of *) must be an lvalue (no constants) * and & are inverses Cancel each other out *&myvar == myvar and &*yptr == yptr 6

Example int f = 300; int *pt = &f; cout<<*pt<<endl; // print the value of f (300) cout<<&pt<<endl; // print the address in which pt pointer is stored at (0012FF6C) cout<<pt<<endl; // print the value of pt which is the address of f (0012FF70) 7

Pointer Operators III Dereferencing a variable that is not a pointer is a syntax error, e.g.: int y = 10; cout << *y; //syntax error Dereferencing a null-pointer is a fatal error (i.e. you program will abort abnormally). int *p; cout<<*p; // the program will run but after the run it will close abnormally The content of a pointer (which is a memory address) is expressed in hexadecimal when you print it on the screen. Some compilers may print it in decimal (machine dependent). You can change the address within the pointer during the program (i.e. you can change the variable to which the pointer is pointing). You must initialize the pointer with the address of a variable with the same data types as the pointer. e.g.: int *p; double y = 10; P = &y; //Syntax error 8

1 // Fig. 5.4: fig05_04.cpp 2 // Using the & and * operators 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 int a; // a is an integer 11 int *aptr; // aptr is a pointer to an integer 12 13 a = 7; 14 aptr = &a; // aptr set to address of a 15 16 cout << "The address of a is " << &a 17 << "\nthe value of aptr is " << aptr; 18 19 cout << "\n\nthe value of a is " << a 20 << "\nthe value of *aptr is " << *aptr; 21 22 cout << "\n\nshowing that * and & are inverses of " 23 << "each other.\n&*aptr = " << &*aptr 24 << "\n*&aptr = " << *&aptr << endl; 25 return 0; 26 } The address of a is 006AFDF4 The value of aptr is 006AFDF4 The value of a is 7 The value of *aptr is 7 Showing that * and & are inverses of each other. &*aptr = 006AFDF4 *&aptr = 006AFDF4 The address of a is the value of aptr. The * operator returns an alias to what its operand points to. aptr points to a, so *aptr returns a. Notice how * and & are inverses The Hashemite University 9

Using the const Qualifier with Pointers I const qualifier: Variable cannot be changed. const used when function does not need to change a variable. Attempting to change a const variable is a syntax error. const pointers: Point to same memory location during the whole program, i.e. the pointer cannot be modified to point to other data. Must be initialized when declared int *const myptr = &x; Pointer points to const data: Means that the data cannot be modified indirectly (i.e. through the pointer) by dereferencing the pointer. 10

Using the const Qualifier with Pointers II Types of const pointers: Non-constant pointer to a non-constant data e.g.: int *myptr = &x; Constant pointer to a non-constant data: default for array names e.g.: int *const myptr = &x; Non-constant pointer to a constant data e.g.: const int *myptr = &x; Constant pointer to a constant data e.g.: const int *const myptr = &x; 11

1 // Fig. 5.13: fig05_13.cpp 2 // Attempting to modify a constant pointer to 3 // non-constant data 4 #include <iostream> 5 6 int main() 7 { 8 int x, y; 9 10 int * const ptr = &x; // ptr is a constant pointer to an 11 // integer. An integer can be modified 12 // through ptr, but ptr always points 13 // to the same memory location. 14 *ptr = 7; 15 ptr = &y; 16 17 return 0; 18 } Changing *ptr is allowed - x is not a constant. Changing ptr is an error - ptr is a constant pointer. Error E2024 Fig05_13.cpp 15: Cannot modify a const object in function main() 12