29
Pointers Pointers Chapter: Chapter: 10 10 Lecture: 37 Lecture: 37 Date: 16.10.2012 Date: 16.10.2012

Lec 37 - pointers

Embed Size (px)

Citation preview

Page 1: Lec 37 -  pointers

PointersPointers

Chapter: Chapter: 1010

Lecture: 37Lecture: 37

Date: 16.10.2012Date: 16.10.2012

Page 2: Lec 37 -  pointers

What are Pointers Used For?What are Pointers Used For?

Accessing array elementsAccessing array elements Passing arguments to a function when the Passing arguments to a function when the

function needs to modify the original function needs to modify the original argumentargument

Passing arrays and strings to functionsPassing arrays and strings to functions Obtaining memory from the systemObtaining memory from the system Creating data structures such as linked listsCreating data structures such as linked lists

Page 3: Lec 37 -  pointers

Memory and AddressesMemory and Addresses

1270

1271

1272

1273

1274

1275

Computer Memory

Page 4: Lec 37 -  pointers

Memory and AddressesMemory and Addresses

1270

1271

1272

1273

1274

1275

Computer Memory

Addresses

Locations

Page 5: Lec 37 -  pointers

Memory and AddressesMemory and Addresses

int IntVar1; //2 bytes

int IntVar2; //2 byte

Page 6: Lec 37 -  pointers

Memory and AddressesMemory and Addresses

1270

1271

1272

1273

1274

1275

Computer Memory

Addresses

Locations

IntVar1

IntVar2

int IntVar1; //2 bytes

int IntVar2; //2 byte

Page 7: Lec 37 -  pointers

Memory and AddressesMemory and Addresses

int IntVar1 = 25;

int IntVar2 = 11;

Page 8: Lec 37 -  pointers

Memory and AddressesMemory and Addresses

1270

1271

1272

1273

1274

1275

Contents/Data

Addresses

Locations

IntVar1

IntVar2

int IntVar1 = 25;

int IntVar2 = 11;

25

11

Page 9: Lec 37 -  pointers
Page 10: Lec 37 -  pointers

Memory and AddressesMemory and Addresses In some cases we may be interested in knowing In some cases we may be interested in knowing

the address where our variable is being stored the address where our variable is being stored during runtime.during runtime.

The address that locates a variable within memory The address that locates a variable within memory is what we call a is what we call a referencereference to that variable. to that variable.

e.g.,e.g.,

& & IntVar;IntVar;

When preceding the name of the variable “IntVar” When preceding the name of the variable “IntVar” with the reference operator (&) we are no longer with the reference operator (&) we are no longer talking about the content of the variable itself, but talking about the content of the variable itself, but about its reference (i.e., its address in memory).about its reference (i.e., its address in memory).

Address-of/reference operator

Page 11: Lec 37 -  pointers

Memory and AddressesMemory and Addresses#include <iostream>#include <iostream>

#include <conio.h>#include <conio.h>

using namespace std;using namespace std;

int main()int main()

{{

int IntVar1; int IntVar1;

int IntVar2;int IntVar2;

cout << &IntVar1 << endl cout << &IntVar1 << endl //print the //print the addressesaddresses

<< &IntVar2 << endl;<< &IntVar2 << endl;

getch();getch();

return 0; }return 0; }

Page 12: Lec 37 -  pointers

Pointer VariablePointer Variable

The variable that stores the reference to The variable that stores the reference to another variable is what we call a another variable is what we call a pointerpointer. .

e.g.,e.g.,

ptr = &InVar;ptr = &InVar;

Page 13: Lec 37 -  pointers

Pointer VariablePointer Variable

The variable that stores the reference to The variable that stores the reference to another variable is what we call a another variable is what we call a pointerpointer. .

e.g.,e.g.,

int int ** ptrptr; ; //variable “ptr” as a //variable “ptr” as a pointer-to “int”pointer-to “int”

ptr = &InVar;ptr = &InVar;

Pointer/Pointer-variable

Pointer-to

Page 14: Lec 37 -  pointers

Accessing AddressesAccessing Addressesint main()int main()

{ int IntVar1 = 25; { int IntVar1 = 25;

int IntVar2 = 11;int IntVar2 = 11;

int* ptr;int* ptr; //pointer to integers//pointer to integers

ptr = &IntVar1; ptr = &IntVar1; //pointer points to IntVar1//pointer points to IntVar1

cout << ptr << endl cout << ptr << endl //print the address of //print the address of IntVar1IntVar1

ptr = &IntVar2 ptr = &IntVar2

cout << ptr << endl cout << ptr << endl //print the address of //print the address of IntVar2IntVar2

getch();getch();

return 0; }return 0; }

Page 15: Lec 37 -  pointers

1270

1271

1272

1273

1274

1275

IntVar1

IntVar2

25

11

1271

ptr points-to to the address of IntVar1

1270

1271

1272

1273

1274

1275

IntVar1

IntVar2

25

11

ptr

1274

ptr

ptr points-to to the address of IntVar2

int* ptr;

ptr = &IntVar1; cout << ptr ;

int* ptr;

ptr = &IntVar2; cout << ptr ;

Page 16: Lec 37 -  pointers

Accessing ContensAccessing Contensint main()int main()

{ int IntVar1 = 25; { int IntVar1 = 25;

int IntVar2 = 11;int IntVar2 = 11;

int* ptr;int* ptr; //pointer to integers//pointer to integers

ptr = &IntVar1;ptr = &IntVar1; //pointer points to IntVar1//pointer points to IntVar1

cout << cout << *ptr *ptr << endl << endl //print the content of //print the content of IntVar1IntVar1

ptr = &IntVar2 ptr = &IntVar2

cout << cout << *ptr *ptr << endl << endl //print the content of //print the content of IntVar2IntVar2

getch();getch();

return 0; }return 0; }

Page 17: Lec 37 -  pointers

IntVar1

IntVar2

25

11

*ptr is 25

IntVar1

IntVar2

25

11

ptr

ptr

*ptr is 11

int* ptr;

ptr = &IntVar1; cout << *ptr ;

int* ptr;

ptr = &IntVar2; cout << *ptr ;

deference /indirection operator. Expression *ptr means the value of the variable pointed to by ptr.

Page 18: Lec 37 -  pointers

Pointer to VoidPointer to Void

The address that is put in a pointer variable The address that is put in a pointer variable must be the same type as the pointer, for must be the same type as the pointer, for example, the address of a float variable can’t example, the address of a float variable can’t be assigned to a pointer to int. be assigned to a pointer to int.

float floVar = 25.67;float floVar = 25.67;

int* ptrInt = &floVar;int* ptrInt = &floVar;

Page 19: Lec 37 -  pointers

Pointer to VoidPointer to Void

The address that is put in a pointer variable must be The address that is put in a pointer variable must be the same type as the pointer, for example, the the same type as the pointer, for example, the address of a float variable can’t be assigned to a address of a float variable can’t be assigned to a pointer to int. pointer to int.

float floVar = 25.67;float floVar = 25.67;

int* ptrInt = &floVar; int* ptrInt = &floVar; //ERROR: can’t assign float* to //ERROR: can’t assign float* to int*int*

Page 20: Lec 37 -  pointers

Pointer to VoidPointer to Void The address that is put in a pointer variable The address that is put in a pointer variable

must be the same type as the pointer, for must be the same type as the pointer, for example, the address of a float variable can’t be example, the address of a float variable can’t be assigned to a pointer to int. assigned to a pointer to int.

float floVar = 25.67;float floVar = 25.67;

int* ptrInt;int* ptrInt;

ptrInt = &floVar; ptrInt = &floVar; //ERROR: can’t assign float* to int*//ERROR: can’t assign float* to int*

Exception to that case is a general-purpose Exception to that case is a general-purpose pointer that can point to any data type, e.g.,pointer that can point to any data type, e.g.,

void* ptrVoid; void* ptrVoid; //pointer to void//pointer to void

Page 21: Lec 37 -  pointers

Pointer to VoidPointer to Void The address that is put in a pointer variable The address that is put in a pointer variable

must be the same type as the pointer, for must be the same type as the pointer, for example, the address of a float variable can’t be example, the address of a float variable can’t be assigned to a pointer to int. assigned to a pointer to int.

float floVar = 25.67;float floVar = 25.67;

int* ptrInt;int* ptrInt;

ptrInt = &floVar; ptrInt = &floVar; //ERROR: can’t assign float* to int*//ERROR: can’t assign float* to int*

Exception to that case is a general-purpose Exception to that case is a general-purpose pointer that can point to any data type, e.g.,pointer that can point to any data type, e.g.,

void* ptrVoid; void* ptrVoid; //pointer to void//pointer to void

ptrVoid = &floVar; ptrVoid = &floVar; //OK//OK

Page 22: Lec 37 -  pointers

Counting by Integers - Counting by Integers - ArraysArrays

Page 23: Lec 37 -  pointers

Passing Arguments to Passing Arguments to FunctionsFunctions

Arguments can be passed to functions in three Arguments can be passed to functions in three different ways: (i) by value, (ii) by reference, and (iii) different ways: (i) by value, (ii) by reference, and (iii) by pointersby pointers

A function can change the values in a calling function A function can change the values in a calling function if the arguments are passed by a reference or by a if the arguments are passed by a reference or by a pointer.pointer.

Page 24: Lec 37 -  pointers

Pass-by-ReferencePass-by-Referencevoid centimize(double& );void centimize(double& );

int main()int main()

{ double var = 2.5; { double var = 2.5;

centimize(var); centimize(var);

cout << var << endl;cout << var << endl;

getch(); return 0; }getch(); return 0; }

void centimize(double& v)void centimize(double& v)

{ v = v * 100; }{ v = v * 100; }

Page 25: Lec 37 -  pointers

Pass-by-PointerPass-by-Pointervoid centimize(doublevoid centimize(double** ); );

int main()int main()

{ double var = 2.5; { double var = 2.5;

centimize(centimize(&&var); var);

cout << var << endl;cout << var << endl;

getch(); return 0; }getch(); return 0; }

void centimize(doublevoid centimize(double** ptrd) ptrd)

{ *ptrd = *ptrd * 100; }{ *ptrd = *ptrd * 100; }

Page 26: Lec 37 -  pointers

Pointer Passed to FunctionPointer Passed to Function

Page 27: Lec 37 -  pointers

Passing Arrays to FunctionPassing Arrays to Functionconst int MAX = 5;const int MAX = 5;

void centimize(double*); //prototypevoid centimize(double*); //prototype

int main()int main()

{ double varray[MAX] = { 10.0, 43.1, 95.9, 59.7, { double varray[MAX] = { 10.0, 43.1, 95.9, 59.7, 87.3 };87.3 };

centimize(varray); centimize(varray);

for(int j=0; j<MAX; j++) for(int j=0; j<MAX; j++)

cout << varray[j] << endl;cout << varray[j] << endl;

getch(); return 0; }getch(); return 0; }

void centimize(double* ptrd)void centimize(double* ptrd)

{ for(int j=0; j<MAX; j++){ for(int j=0; j<MAX; j++)

*ptrd++ = *ptrd * 2.54; } *ptrd++ = *ptrd * 2.54; } //*ptrd++ = //*ptrd++ = *(ptrd++)*(ptrd++)

Page 28: Lec 37 -  pointers

Pointer Passed to FunctionPointer Passed to Function

Page 29: Lec 37 -  pointers

Linked ListLinked List

Linked list is another way to store data Linked list is another way to store data besides storing data in arrays.besides storing data in arrays.

However, arrays suffer from the necessity However, arrays suffer from the necessity to declare a fixed-size array before to declare a fixed-size array before running the program.running the program.