lec 38.39 - pointers

Post on 22-May-2015

81 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

PointersPointers

Chapter: Chapter: 1010

Lecture: 38 and 39Lecture: 38 and 39

Date: 18.10.2012Date: 18.10.2012

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; }

Accessing ContentsAccessing Contentsint 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; }

Array Accessing Using IndexArray Accessing Using Index

int main()int main()

{ {

int intarray[5] = { 31, 54, 77, 52, 93 };int intarray[5] = { 31, 54, 77, 52, 93 };

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

cout << intarray[j] << endl; cout << intarray[j] << endl;

getch();getch();

return 0;return 0;

}}

Array Accessing Using Array Accessing Using PointersPointers

int main()int main()

{ {

int intarray[5] = { 31, 54, 77, 52, 93 };int intarray[5] = { 31, 54, 77, 52, 93 };

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

cout << cout << *( intarray + j )*( intarray + j ) << endl; << endl;

getch();getch();

return 0;return 0;

}}

Counting by Integers - Counting by Integers - ArraysArrays

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.

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; }

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; }

Pointer Passed to FunctionPointer Passed to Function

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

void centimize(double*); void centimize(double*);

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++)

Pointer Passed to FunctionPointer Passed to Function

Assignment Assignment # 02# 02

(i)(i) Write a note on sorting with an example.Write a note on sorting with an example.

(ii)(ii) What is bubble sort? Write a program What is bubble sort? Write a program implementing bubble sort using pointers.implementing bubble sort using pointers.

Assignment Assignment # 02# 02

(i)(i) Write a note on sorting with an exampleWrite a note on sorting with an example

(ii)(ii) What is bubble sort? Write a program What is bubble sort? Write a program implementing bubble sort using pointers.implementing bubble sort using pointers.

Viva voce for the first and second Viva voce for the first and second assignment is scheduled to be assignment is scheduled to be conducted on 24conducted on 24thth October. October.

top related