Lec 36 - pointers
-
Upload
princess-sam -
Category
Documents
-
view
252 -
download
1
description
Transcript of Lec 36 - pointers
PointersPointers
Chapter: Chapter: 1010
Lecture: 36Lecture: 36
Date: 15.10.2012Date: 15.10.2012
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
Memory and AddressesMemory and Addresses
1270
1271
1272
1273
1274
1275
Computer Memory
Memory and AddressesMemory and Addresses
1270
1271
1272
1273
1274
1275
Computer Memory
Addresses
Locations
Memory and AddressesMemory and Addresses
int IntVar1; //2 bytes
int IntVar2; //2 byte
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
Memory and AddressesMemory and Addresses
int IntVar1 = 25;
int IntVar2 = 11;
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
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
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; }
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;
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
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; }
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 ;
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; }
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.
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;
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*
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
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
Counting by Integers - Counting by Integers - ArraysArrays