22
Pointers Pointers Chapter: Chapter: 10 10 Lecture: 36 Lecture: 36 Date: 15.10.2012 Date: 15.10.2012

Lec 36 - pointers

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Lec 36 -  pointers

PointersPointers

Chapter: Chapter: 1010

Lecture: 36Lecture: 36

Date: 15.10.2012Date: 15.10.2012

Page 2: Lec 36 -  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 36 -  pointers

Memory and AddressesMemory and Addresses

1270

1271

1272

1273

1274

1275

Computer Memory

Page 4: Lec 36 -  pointers

Memory and AddressesMemory and Addresses

1270

1271

1272

1273

1274

1275

Computer Memory

Addresses

Locations

Page 5: Lec 36 -  pointers

Memory and AddressesMemory and Addresses

int IntVar1; //2 bytes

int IntVar2; //2 byte

Page 6: Lec 36 -  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 36 -  pointers

Memory and AddressesMemory and Addresses

int IntVar1 = 25;

int IntVar2 = 11;

Page 8: Lec 36 -  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 36 -  pointers
Page 10: Lec 36 -  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 36 -  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 36 -  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 36 -  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 36 -  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 36 -  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 36 -  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 36 -  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 36 -  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 36 -  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 36 -  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 36 -  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 36 -  pointers

Counting by Integers - Counting by Integers - ArraysArrays