pointers - lecture 2 section 2

35
Pointers Lecture 2 Section 2.4 Robb T. Koether Hampden-Sydney College Fri, Jan 15, 2010 Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 1 / 35

Upload: others

Post on 12-Sep-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pointers - Lecture 2 Section 2

PointersLecture 2

Section 2.4

Robb T. Koether

Hampden-Sydney College

Fri, Jan 15, 2010

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 1 / 35

Page 2: Pointers - Lecture 2 Section 2

Outline

1 Introduction

2 Pointer ArithmeticPointer + intPointer - Pointer

3 Pointers and Arrays

4 Pointers as Function Parameters

5 Assignment

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 2 / 35

Page 3: Pointers - Lecture 2 Section 2

Outline

1 Introduction

2 Pointer ArithmeticPointer + intPointer - Pointer

3 Pointers and Arrays

4 Pointers as Function Parameters

5 Assignment

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 3 / 35

Page 4: Pointers - Lecture 2 Section 2

Big Endian and Little Endian

Suppose we make the assignmentint i = 0x25116634;

If the architecture is Big Endian, then it is stored as

25 346611"Big" end first

If the architecture is Little Endian, then it is stored as

34 251166"Little" end first

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 4 / 35

Page 5: Pointers - Lecture 2 Section 2

Big Endian and Little Endian

Definition (Big Endian, Little Endian)If a processor has Big Endian architecture, then it stores thehighest-order byte of the value in the lowest byte address.If a processor has Little Endian architecture, then it stores thelowest-order byte of the value in the lowest byte address.

Multi-byte objects are addressed by their lowest byte address.

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 5 / 35

Page 6: Pointers - Lecture 2 Section 2

Example

Example (Endianness.cpp)The code.The executable.

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 6 / 35

Page 7: Pointers - Lecture 2 Section 2

Outline

1 Introduction

2 Pointer ArithmeticPointer + intPointer - Pointer

3 Pointers and Arrays

4 Pointers as Function Parameters

5 Assignment

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 7 / 35

Page 8: Pointers - Lecture 2 Section 2

Outline

1 Introduction

2 Pointer ArithmeticPointer + intPointer - Pointer

3 Pointers and Arrays

4 Pointers as Function Parameters

5 Assignment

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 8 / 35

Page 9: Pointers - Lecture 2 Section 2

Pointer + int

It is permissible to add an integer to a pointer.int i = 123;int* p = &i;int* q = p + 1;

The integer is multiplied by the size of the object pointed to.That value is added to the address.The result is a pointer of the same type.

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 9 / 35

Page 10: Pointers - Lecture 2 Section 2

Pointer + int

The integer is interpreted as the number of objects of that typebetween the original address and the new address.The resulting address is interpreted as a pointer to the objectlocated the specified number of objects to the right, as in an array.

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 10 / 35

Page 11: Pointers - Lecture 2 Section 2

Pointer Arithmetic

int i = 123; // Addr of i is 0x0100int* p = &i; // p = 0x0100char* q = (char*)&i; // q = 0x0100short* r = (short*)&i; // r = 0x0100

0x0100(i)

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 11 / 35

Page 12: Pointers - Lecture 2 Section 2

Pointer Arithmetic

int i = 123; // Addr of i is 0x0100int* p = &i; // p = 0x0100char* q = (char*)&i; // q = 0x0100short* r = (short*)&i; // r = 0x0100

0x0100(i)

p p + 1 p + 2p - 1

0x0104 0x01080x00fc

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 12 / 35

Page 13: Pointers - Lecture 2 Section 2

Pointer Arithmetic

int i = 123; // Addr of i is 0x0100int* p = &i; // p = 0x0100char* q = (char*)&i; // q = 0x0100short* r = (short*)&i; // r = 0x0100

0x0100(i)

q q + 1 q + 2q - 1

0x0104 0x01080x00fc

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 13 / 35

Page 14: Pointers - Lecture 2 Section 2

Pointer Arithmetic

int i = 123; // Addr of i is 0x0100int* p = &i; // p = 0x0100char* q = (char*)&i; // q = 0x0100short* r = (short*)&i; // r = 0x0100

0x0100(i)

r r + 1 r + 2r - 1

0x0104 0x01080x00fc

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 14 / 35

Page 15: Pointers - Lecture 2 Section 2

Example

Example (Ptr Plus Int.cpp)The code.The executable.

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 15 / 35

Page 16: Pointers - Lecture 2 Section 2

Outline

1 Introduction

2 Pointer ArithmeticPointer + intPointer - Pointer

3 Pointers and Arrays

4 Pointers as Function Parameters

5 Assignment

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 16 / 35

Page 17: Pointers - Lecture 2 Section 2

Pointer Subtraction

Subtraction of one pointer from another pointer (of the same type)is permitted.

I Take the difference of the addresses.I Divide it by the size of the object pointed to.I The result is an int.

Interpret the result as the number of objects of that type betweentwo addresses.

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 17 / 35

Page 18: Pointers - Lecture 2 Section 2

Pointer Subtraction

Example (Pointer Subtraction)int* p;int* q;int a = q - p;

p q

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 18 / 35

Page 19: Pointers - Lecture 2 Section 2

Pointer Subtraction

Example (Pointer Subtraction)int* p;int* q;int a = q - p;

p qq - p = 5

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 19 / 35

Page 20: Pointers - Lecture 2 Section 2

Example

Example (Ptr Minus Ptr.cpp)The code.The executable.

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 20 / 35

Page 21: Pointers - Lecture 2 Section 2

Pointer Arithmetic

Exampleint a = (p2 - p1) + 5;int b = p2 - (p1 - 5);int c = (p2 + 5) - p1;

These are legal operations.Are they equivalent?

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 21 / 35

Page 22: Pointers - Lecture 2 Section 2

Pointer Arithmetic

Example (Illegal Addition)int* mid = (p2 + p1)/2;

Pointer addition is illegal.How can we obtain a pointer to the object halfway between p1and p2?

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 22 / 35

Page 23: Pointers - Lecture 2 Section 2

Outline

1 Introduction

2 Pointer ArithmeticPointer + intPointer - Pointer

3 Pointers and Arrays

4 Pointers as Function Parameters

5 Assignment

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 23 / 35

Page 24: Pointers - Lecture 2 Section 2

Pointers and Constants

Example (Pointers and Constants)int const i = 123;int j = 456;int const * iptr = &i;int * const iptr2 = &j;int const * const iptr3 = &i;

A pointer may point to a constant – the object pointed to cannot bechanged.A pointer itself may be constant – the pointer cannot be changed.In fact, a constant pointer may point to a constant object!

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 24 / 35

Page 25: Pointers - Lecture 2 Section 2

An Array Name as a Pointer

Example (Pointers and Arrays)int a[3] = {10, 20, 30};int* iptr = a;cout << *iptr << endl;cout << *(iptr + 1) << endl;cout << *(iptr + 2) << endl;

An array name represents a pointer that points to the first memberof the array.The array name is a constant pointer; its value cannot bechanged.An array name may be assigned to a pointer of the same type.

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 25 / 35

Page 26: Pointers - Lecture 2 Section 2

A Pointer as an Array Name

Example (Pointers and Arrays)int a[3] = {10, 20, 30};int* iptr = a;cout << iptr[0] << endl;cout << iptr[1] << endl;cout << iptr[2] << endl;

A pointer name may be indexed.p[i] is equivalent to *(p + i).

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 26 / 35

Page 27: Pointers - Lecture 2 Section 2

Example

Example (Pointer Loop.cpp)The code.The executable.

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 27 / 35

Page 28: Pointers - Lecture 2 Section 2

Outline

1 Introduction

2 Pointer ArithmeticPointer + intPointer - Pointer

3 Pointers and Arrays

4 Pointers as Function Parameters

5 Assignment

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 28 / 35

Page 29: Pointers - Lecture 2 Section 2

Pointers as Function Parameters

In CS I, we learn to pass objects to function by reference.The intention is to allow the object to be modified by the function.Before pass-by-reference was introduced, the same wasaccomplished by passing a pointer.To access the object form within the function, the pointerparameter must be dereferenced.

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 29 / 35

Page 30: Pointers - Lecture 2 Section 2

Example

Example (Pointer Parameter)void swap(int* a, int* b){

int temp = *a;

*a = *b;

*b = temp;return;

}

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 30 / 35

Page 31: Pointers - Lecture 2 Section 2

Example

Example (Pass By Pointer.cpp)The code.The executable.

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 31 / 35

Page 32: Pointers - Lecture 2 Section 2

Arrays as Function Parameters

When an array is “passed” as a parameter, the name of the arrayis passed, as a pointer.Thus, the function receives a pointer to the first element of thearray.This is far more efficient than copying the entire array.

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 32 / 35

Page 33: Pointers - Lecture 2 Section 2

Example

Example (Array Parameter)void Sort(int list[], int size){

for (int i = 0; i < size - 1; i++){

int* q = list;while (q < list + size - 1){

if (*q > *(q + 1))Swap(q, q + 1);

q++;}

}return;

}

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 33 / 35

Page 34: Pointers - Lecture 2 Section 2

Outline

1 Introduction

2 Pointer ArithmeticPointer + intPointer - Pointer

3 Pointers and Arrays

4 Pointers as Function Parameters

5 Assignment

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 34 / 35

Page 35: Pointers - Lecture 2 Section 2

Assignment

HomeworkRead Section 2.4, pages 63 - 71.Read Section 3.2, pages 85 - 97.

Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 15, 2010 35 / 35