Thursday, January 11, 2007 Harrisberger's Fourth Law of the Lab: Experience is directly proportional to the amount of equipment ruined Harrisberger's Fourth.

Slides:



Advertisements
Similar presentations
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Advertisements

1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
1 Pointers (Walls & Mirrors - Beginning of Chapter 4)
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
CS 192 Lecture 12 Winter 2003 December 31, January 1, 2004 Dr. Shafay Shamail.
Friday, January 12, 2007 Parkinson's Law “Work expands so as to fill the time available for its completion."
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 (Pointers) Lecture 1 –Follow class notes –Some.
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.
Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
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.
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.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
 2006 Pearson Education, Inc. All rights reserved Pointers.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
Pointer Data Type and Pointer Variables
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Chapter 6 Pointers C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
(continue) © by Pearson Education, Inc. All Rights Reserved.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
[S. Uludag] CIS / CSC 175 Problem Solving and Programming I Winter 2010 Suleyman Uludag Department of Computer Science, Engineering and Physics (CSEP)
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.
CS102 Introduction to Computer Programming Chapter 9 Pointers.
Review 1 List Data Structure List operations List Implementation Array Linked List.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
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.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Pointers What is the data type of pointer variables?
Computer Skills2 for Scientific Colleges
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
Lecture 6 C++ Programming
8 Pointers.
Pointer Data Type and Pointer Variables
Pointers and Dynamic Variables
Computer Skills2 for Scientific Colleges
Pointers Kingdom of Saudi Arabia
Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
CISC181 Introduction to Computer Science Dr
Programming fundamentals 2 Chapter 3:Pointer
Presentation transcript:

Thursday, January 11, 2007 Harrisberger's Fourth Law of the Lab: Experience is directly proportional to the amount of equipment ruined Harrisberger's Fourth Law of the Lab: Experience is directly proportional to the amount of equipment ruined.

Most of C++’s power is derived from pointers. They allow different sections of code to share information easily. Pointers enable complex data structures like linked lists. Pointers

Assumptions: characters are one byte in length integers are four bytes long floats are four bytes long doubles are eight bytes long Pointers

A pointer is a variable that holds a memory address. Pointers

int x=5; int *xptr; 5 x of type int0x0012F578 0x0012F690xptr of type int*

int x=5; int *xptr; xptr=&x; //points to x 5 0x0012F578 x of type int0x0012F578 0x0012F690xptr of type int*

int x=5; int *xptr; xptr=&x; //points to x 5 x of type int0x0012F578 xptr of type int*

int x=5; int *xptr; xptr=&x; //points to x 5 x of type intxptr of type int*

int x=5; int *xptr; xptr=&x; //points to x The most common error is forgetting to initialize the pointer 5 x of type intxptr of type int*

There are two special operators that are used with pointers: * and & The & is a unary operator that returns the memory address of its operand. int balance = 350; int *balptr; balptr = &balance; Pointer Operators

The second operator * is the complement of &. It is a unary operator that returns the value of variable located at address specified by its operand. int balance = 350; int *balptr; balptr = &balance; int value; value = *balptr; //what does value contain? Pointer Operators

& address of operator * value of pointee (de-referencing operator) Pointer Operators

When a pointer is first allocated it does not point to anything. Trying to de-reference an un-initialized pointer is a serious runtime error. If you are lucky the dereference will crash or halt immediately. If you are unlucky it will corrupt a random area of memory – so that things go wrong after some indefinite time. Pointers

Make a memory drawing! Pointers

int main() { int balance; int *balptr; int value; balance = 3200; balptr = &balance; value = *balptr; cout << "balance is: " << value << '\n'; cout<< "Memory address where balance is stored is: " <<balptr<<endl; return 0; } Pointer Operators

Output balance is: 3200 Memory address where balance is stored is: 0012FF7C (Note memory address may be different when you run it on your machine)

§Another example §Three things to remember…

int a, b; int *aptr, bptr; //Be careful!

value = *balptr; The compiler transfers the proper number of bytes according to base type. int *p; double f; //... p = &f; // ERROR Base Type

int *p; int x=12; p=&x; cout<<x<<endl; //Assign a value to the location pointed to by p *p = 101; cout<<x<<endl;//what is value of x? cout<<*p<<endl; cout<<p<<endl; cout<<&x<<endl; Assigning values through a pointer

int *p; int x=12; p=&x; cout<<x<<endl;//prints 12 //Assign a value to the location pointed to by p *p = 101; cout<<x<<endl; //prints 101 cout<<*p<<endl; //prints 101 cout<<p<<endl; //prints 0012FF78 cout<<&x<<endl; //prints 0012FF78 (Note memory address may be different when you run it on your machine) Assigning values through a pointer

(*p)++; //increment value at the location pointed to by p int main() { int *p, num; p = &num; *p = 454; cout << num << ' '; (*p)++; /*parentheses are necessary because * operator has lower precedence than ++ operator */ cout << num << ' '; (*p)--; cout << num << '\n'; return 0; } //Output? Assigning values through a pointer

//Output is int main() { int *p, num; p = &num; *p = 454; cout << num << ' '; (*p)++; cout << num << ' '; (*p)--; cout << num << '\n'; return 0; } Assigning values through a pointer

There are only four arithmetic operators that can be used on pointers: ++, --, +, - Let p1 be an integer pointer which contains the address 5000 p1++; Each time p1 is incremented, it shall point to the next integer. p1--; Pointer Arithmetic

There are only four arithmetic operators that can be used on pointers: ++, --, +, - Let p1 be an integer pointer which contains the address 5000 p1++;// now p1 will be 5004 Each time p1 is incremented, it shall point to the next integer. p1--; will cause p1 to be 4996 if initially it was Pointer Arithmetic

int main() { int *aptr, a[5]; double *bptr, b[5]; int i; aptr = a; bptr = b; for(i=0; i<5; i++){ aptr = a+i; bptr = b+i; cout << aptr << " " << bptr << endl; } return 0; } Pointer Arithmetic

0x0012FEB8 0x0012FE7C 0x0012FEBC 0x0012FE84 0x0012FEC0 0x0012FE8C 0x0012FEC4 0x0012FE94 0x0012FEC8 0x0012FE9C Pointer Arithmetic

Let p1 be an char pointer which contains the address 4000 p1++;// now p1 will be 4001 Each time p1 is incremented, it shall point to the next character. p1--; will cause p1 to be 3999 if initially it was Pointer of type other than char shall increase or decrease by length of base type. Pointer Arithmetic

You cannot add two pointers You can subtract two pointers (if they are of same base type). Other than addition or subtraction of a pointer and an integer, or the subtraction of two pointers, no other arithmetic operations can be performed on pointers. You cannot add or subtract float or double values. Pointer Arithmetic

int main() { int *aptr, a[5]; double *bptr, b[5]; int i; aptr = a; bptr = b; for(i=0; i<5; i++){ a = aptr+i; b = bptr+i; cout << a << " " << b << endl; } return 0; } What is wrong here?

In C++, there is a close relationship between pointers and arrays. Pointers and Arrays

In C++, there is a close relationship between pointers and arrays. Name of array, without an index, is a pointer to first element of the array. Pointers and Arrays

int main() { int *i; int i_array[4]={55, 26, 17, 68}; i=i_array; cout << i << " " << i_array << “\n”; cout << i[0] << " " << i_array[0] << “\n”; cout << *(i+1) << " " << i_array[1] <<“\n”; return 0; } //Output? Pointers and Arrays

Output is: 0012FF6C Pointers and Arrays

int i1[4]={55,26,17,68}; int i2[4]={11, 2, 53, 14}; i2=i1; //NOT ALLOWED char str1[]=“i am a string”; char str2[]=“i am a string”; Pointers may be compared in C++ using relational operators like >, >=, <, <=, == etc, but they must have some relationship to be meaningful Pointers and Arrays

int i1[4]={55,26,17,68}; int i2[4]={11, 2, 53, 14}; i2=i1; //NOT ALLOWED Why? /*name of array is a constant that points to beginning of array*/ char str1[]=“i am a string”; char str2[]=“i am a string”; str1==str2; //WRONG way of string comparison Pointers may be compared in C++ using relational operators like >, >=, <, <=, == etc, but they must have some relationship to be meaningful Pointers and Arrays

int i1[4]={55,26,17,68}; int i2[4]={11,2,53,14}; int *p1; p1 = i1; int *p2; p2 = p1; //this is ok Pointers and Arrays

int *y_ptr, y; //y is of type int // y_ptr is pointer to int y=45; y_ptr=&y; int *y_ptr, *another_ptr, y=45; y_ptr=&y; another_ptr=&y; /*what is *another_ptr what is *y_ptr*/ Pointers

int b[] = {10, 20, 30, 40}; int *bPtr; bPtr = b; // set bPtr to point to array b cout << "Array b printed with:" << endl << "Array subscript notation" << endl; for (int i = 0; i < 4; i++) cout <<b[i] << endl; cout << "Pointer subscript notation" << endl; for (i = 0; i < 4; i++) cout << bPtr[i] << endl; //Another way to do this? Using subscripting and pointer notations with arrays

int offset; cout << "Pointer/offset notation where" << endl << "the pointer is the array name" << endl; for(offset = 0; offset < 4; offset++) cout << *(b + offset) << endl; cout <<"Pointer/offset notation" << endl; for(offset = 0; offset < 4; offset++) cout << *(bPtr + offset) << endl; Using subscripting and pointer notations with arrays

int *i; int int_array[4]={5,6,7,8}; i=int_array; cout << i << " " << int_array<<endl; cout << i[0] << " " << int_array[0]<< endl ; cout << &i[0] << " " << &int_array[0]<< endl; Using subscripting and pointer notations with arrays

0x0012FEBC 5 0x0012FEBC Using subscripting and pointer notations with arrays

int a[5]={12, 3, 45, 5, 8}; int *ptr; ptr=a; cout<<a[0]<<endl; cout<<ptr[0]<<endl; cout<<*(a+0)<<endl; cout<<*(ptr+0)<<endl; cout<<a[1]<<endl; cout<<ptr[1]<<endl; cout<<*(a+1)<<endl; cout<<*(ptr+1)<<endl; Using subscripting and pointer notations with arrays

int a[5]={12, 3, 45, 5, 8}; int *ptr; ptr=a; cout<<a[0]<<endl; //prints 12 cout<<ptr[0]<<endl; //prints 12 cout<<*(a+0)<<endl; //prints 12 cout<<*(ptr+0)<<endl; //prints 12 cout<<a[1]<<endl; //prints 3 cout<<ptr[1]<<endl; //prints 3 cout<<*(a+1)<<endl; //prints 3 cout<<*(ptr+1)<<endl; //prints 3 Using subscripting and pointer notations with arrays

int i1[4]={55, 26, 17, 68}; What is wrong with the following statement? i1++; //The following is ok int *i_ptr; i_ptr = i1; i_ptr++; cout<<*i_ptr; //The following is ok *(i1+3) = 100; // This is OK because i1 has not changed Pointers and Arrays

int b[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; int *bPtr; bPtr= b; // set bPtr to point to array b /* set b2Ptr to point to sixth element of array b */ int *b2ptr; b2ptr = b+5; //Remember pointer arithmetic? cout << "b[" << 5 << "] = " << b[5] << endl; cout << "*b2ptr= " << *b2ptr << endl; cout << "(b2ptr - bPtr) = " << (b2ptr - bPtr); Pointers and Arrays

int b[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; int *bPtr; bPtr= b; // set bPtr to point to array b /* set b2Ptr to point to sixth element of array b */ int *b2ptr; b2ptr = b+5; //Remember pointer arithmetic? cout << "b[" << 5 << "] = " << b[5] << endl; cout << "*b2ptr= " << *b2ptr << endl; cout << "(b2ptr - bPtr) = " << (b2ptr - bPtr) << endl; Output is: b[5] = 60 *b2ptr= 60 (b2ptr - bPtr) = 5 Pointers and Arrays