Friday, January 12, 2007 Parkinson's Law “Work expands so as to fill the time available for its completion."

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.
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.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.
Thursday, January 11, 2007 Harrisberger's Fourth Law of the Lab: Experience is directly proportional to the amount of equipment ruined Harrisberger's Fourth.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Tuesday, January 16, 2007 How would a car function if it were designed like a computer? Occasionally, executing a maneuver would cause your car to stop.
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.
CS1061 C Programming Lecture 13: Pointers A. O’Riordan, 2004.
 2006 Pearson Education, Inc. All rights reserved Pointers.
Pointers 1 Pointers Pointers  In chapter three we looked at passing parameters to functions using call by value, and using call by reference, using reference.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Esempi Puntatori e Stringhe1 // This program puts values into an array, sorts the values into // ascending order, and prints the resulting array. #include.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
C++ Pointers Copies from SEE C++ programming course and from Starting Out with C++: Early Objects, 8/E by Tony Gaddis, Judy Walters and Godfrey Muganda.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
CS102 Introduction to Computer Programming Chapter 9 Pointers.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5: Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
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.
1 Lecture 12 Pointers and Strings Section 5.4, ,
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
 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.
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.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Slide 1 of 36. Attributes Associated with Every Variable Data type Data type Actual value stored in variable Actual value stored in variable Address of.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Lecture 9 - Pointers 1. Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference Pointer.
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 Data Structures CSI 312 CSI Dept. Dr. Yousef Qawqzeh.
Pointers What is the data type of pointer variables?
Computer Skills2 for Scientific Colleges
Chapter 7 - Pointers Outline 7.1 Introduction
Pointers & Arrays.
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
8 Pointers.
Programming fundamentals 2 Chapter 2:Pointer
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.
Pointers & Arrays.
CISC181 Introduction to Computer Science Dr
Pointers.
Programming fundamentals 2 Chapter 3:Pointer
Presentation transcript:

Friday, January 12, 2007 Parkinson's Law “Work expands so as to fill the time available for its completion."

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”; str1==str2; //WRONG 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*/ SELF TEST: 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; SELF TEST: 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 SELF TEST: 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

int main() { int a[3]={10, 20, 30}; double b[3]={10.5, 20.5, 30.5}; int *i; double *f; int x, size=3; i = a; f = b; for(x=0; x<size; x++) { cout << i+x << " " << f+x << '\n'; cout <<*( i+x) << " " <<*( f+x) << '\n'; cout << *i+x << " " << *f+x << '\n'; } return 0; } //pointer-add example Pointer Arithmetic

Output: 0x0012FF74 0x0012FF5C x0012FF78 0x0012FF x0012FF7C 0x0012FF6C Pointer Arithmetic

int a[3]={1, 5, 9}; int *i; i = a; what is the difference between *i++ and (*i)++ and *(i++)? cout<<(*i)++; // display and then increment pointee cout<<++(*i); // increment pointee and then display cout<<*(i++); // display and then increment pointer cout<<*(++i); // increment pointer and then display Pointer Arithmetic

int a[3]={1, 5, 9}; int *i; i = a; cout<<(*i)++; // value is incremented Pointer Arithmetic

int a[3]={1, 5, 9}; int *i; i = a; cout<<(*i)++; // value is incremented //prints 1 Pointer Arithmetic

int a[3]={1, 5, 9}; int *i; i = a; cout<<++(*i); // value is incremented Pointer Arithmetic

int a[3]={1, 5, 9}; int *i; i = a; cout<<++(*i); // value is incremented //prints 2 Pointer Arithmetic

int a[3]={1, 5, 9}; int *i; i = a; cout<<*(i++); // value is incremented Pointer Arithmetic

int a[3]={1, 5, 9}; int *i; i = a; cout<<*(i++); // value is incremented //prints 1 Pointer Arithmetic

int a[3]={1, 5, 9}; int *i; i = a; cout<<*(++i); // value is incremented Pointer Arithmetic

int a[3]={1, 5, 9}; int *i; i = a; cout<<*(++i); // value is incremented //prints 5 Pointer Arithmetic

int a[3]={1, 5, 9}; int *i; i = a; What is output if the statement are executed one after the other? cout<<(*i)++; cout<<++(*i); cout<<*(i++); cout<<*(++i); Pointer Arithmetic

int a[3]={1, 5, 9}; int *i; i = a; What is output if the statement are executed one after the other? cout<<(*i)++; // 1 cout<<++(*i); // 3 cout<<*(i++); // 3 cout<<*(++i); // 9 Pointer Arithmetic

What is wrong here? int* my_Ptr; *my_Ptr=32; Self Test

int num[10]; int *start, *end; start = num; end = &num[9]; while(start!=end) { cout << "Enter a number: "; cin >> *start; start++; } start = num; /* reset the starting pointer */ while(start!=end) { cout << *start << ' '; start++; } Comparing Pointers

int x[ ] = {1, 3, 5, 7, 9, 11}; *x = *(x + *x) + *x; cout<<*x; Pointers

int x[ ] = {1, 3, 5, 7, 9, 11}; *x = *(x + *x) + *x; cout<<*x; // prints 4 Pointers

int *intPtr; int i[10]={10,11,12,13,14,15,16,17,18,19}; char *charPtr; char c[]="We are testing char pointers"; intPtr = i; charPtr = c; cout << i <<" "<< c << endl; cout << intPtr << " " << charPtr << '\n'; cout << *intPtr <<" "<< i[0] << '\n'; cout << *charPtr <<" "<< c[0] << '\n'; char pointers

0x0012FF54 We are testing char pointers 10 W char pointers

char n[25] = "Pointers Are Funny!!"; char *name, *temp; name = n; temp = name+2; cout << n << endl; cout << name << endl; cout << *name << endl; cout << *(temp) << endl; cout << n[10] << endl; cout << ++name << endl; cout << ++(*n) << endl; cout << *(temp) << endl; cout << ++name << endl; cout << *name << endl; cout << n + 15 << endl; cout << name + 15 << endl; cout << ++name << endl; cout << name[2] << endl; Make a memory drawing!

Pointers Are Funny!! P i r ointers Are Funny!! Q i inters Are Funny!! i nny!! y!! nters Are Funny!! e

char n[25] = "Pointers Are Funny!!"; char *name; name = n; cout << n << endl; cout << name << endl; cout << *name << endl; cout << *(name+2) << endl; cout << n[10] << endl; cout << ++name << endl; cout << ++(*n) << endl; cout << *(n+2) << endl; cout << ++name << endl; cout << *name << endl; cout << n + 15 << endl; cout << name + 15 << endl; cout << ++name << endl; cout << name[2] << endl; Make a memory drawing! Self Test