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

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

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Thursday, January 11, 2007 Harrisberger's Fourth Law of the Lab: Experience is directly proportional to the amount of equipment ruined Harrisberger's Fourth.

Thursday, January 11, 2007

Harrisberger's Fourth Law of the Lab:Harrisberger's Fourth Law of the Lab: Experience is directly Experience is directly

proportional to the amount of proportional to the amount of equipment ruinedequipment ruined.

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

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

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

Assumptions:characters are one byte in lengthintegers are four bytes longfloats are four bytes longdoubles are eight bytes long

Pointers

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

A pointer is a variable that holds a memory address.

Pointers

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

int x=5;

int *xptr;

5 x of type int0x0012F578

0x0012F690 xptr of type int*

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

int x=5;

int *xptr;

xptr=&x; //points to x

5

0x0012F578

x of type int0x0012F578

0x0012F690 xptr of type int*

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

int x=5;

int *xptr;

xptr=&x; //points to x

5 x of type int0x0012F578

xptr of type int*

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

int x=5;

int *xptr;

xptr=&x; //points to x

5

x of type intxptr of type int*

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

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*

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

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

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

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

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

& address of operator

* value of pointee(de-referencing operator)

Pointer Operators

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

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

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

Make a memory drawing!

Pointers

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

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

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

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)

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

Another example

Three things to remember…

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

int a, b;

int *aptr, bptr; //Be careful!

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

value = *balptr;The compiler transfers the proper number of bytes according to base type.

int *p;double f;// ...p = &f; // ERROR

Base Type

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

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

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

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 101cout<<*p<<endl; //prints 101cout<<p<<endl; //prints 0012FF78cout<<&x<<endl; //prints 0012FF78

(Note memory address may be different when you run it on your machine)

Assigning values through a pointer

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

(*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

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

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

Assigning values through a pointer

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

There are only four arithmetic operators that can be used on pointers: ++, --, +, -

Let p1 be an integer pointer which contains the address 5000p1++;

Each time p1 is incremented, it shall point to the next integer.p1--;

Pointer Arithmetic

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

There are only four arithmetic operators that can be used on pointers: ++, --, +, -

Let p1 be an integer pointer which contains the address 5000p1++; // 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 5000.

Pointer Arithmetic

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

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

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

0x0012FEB8 0x0012FE7C0x0012FEBC 0x0012FE840x0012FEC0 0x0012FE8C0x0012FEC4 0x0012FE940x0012FEC8 0x0012FE9C

Pointer Arithmetic

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

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

4000.

Pointer of type other than char shall increase

or decrease by length of base type.

Pointer Arithmetic

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

•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

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

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?

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

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

Pointers and Arrays

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

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

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

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

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

Output is:

0012FF6C 0012FF6C55 5526 26

Pointers and Arrays

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

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

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

int i1[4]={55,26,17,68};int i2[4]={11, 2, 53, 14};i2=i1; //NOT ALLOWEDWhy? /*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

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

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

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

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

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

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

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

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

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

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

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

0x0012FEBC 0x0012FEBC5 50x0012FEBC 0x0012FEBC

Using subscripting and pointer notations with arrays

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

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

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

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

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

int i1[4]={55, 26, 17, 68};What is wrong with the following statement?i1++;

//The following is okint *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

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

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

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

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