pointer in c++ dr. ahmed telba. topics covered introduction to pointers pointers and arrays...

57
Pointer in C++ Dr. Ahmed Telba

Upload: jessie-willis

Post on 18-Jan-2016

251 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Pointer in C++

Dr. Ahmed Telba

Page 2: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Topics Covered

• Introduction to Pointers• Pointers and arrays• Character Pointers, • Arrays and Strings• Examples

Page 3: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Introduction to Pointers• When we declare a variable some memory is allocated for it. The memory location

can be referenced through the identifier “i”. Thus, we have two properties for any variable : its address and its data value. The address of the variable can be accessed through the referencing operator “&”. “&i” gives the memory location where the data value for “i” is stored.

• A pointer variable is one that stores an address. We can declare pointers as follows int* p; .This means that p stores the address of a variable of type int.

Page 4: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Introduction to Pointers• Q: Why is it important to declare the type of the variable that a pointer points to?

Aren’t all addresses of the same length?• A: It’s true that all addresses are of the same length, however when we perform an

operation of the type “p++” where “p” is a pointer variable, for this operation to make sense the compiler needs to know the data type of the variable “p” points to. If “p” is a character pointer then “p++” will increment “p” by one byte (typically), if “p” were an integer pointer its value on “p++” would be incremented by 2 bytes (typically).

Page 5: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Introduction to Pointers• Summary of what was learnt so far:

– Pointer is a data type that stores addresses, it is declared as follows: int* a; char* p; etc.

– The value stored in a pointer p can be accessed through the dereferencing operator “*”.– The address of a memory location of a variable can be accessed through the reference

operator “&”.– Pointer arithmetic: only addition and subtraction are allowed.

Page 6: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Pointers and Arrays• The concept of array is very similar to the concept of pointer. The identifier of an

array actually a pointer that holds the address of the first element of the array.• Therefore if you have two declarations as follows:

– “int a[10];” “int* p;” then the assignment “p = a;” is perfectly valid– Also “*(a+4)” and “a[4]” are equivalent as are “*(p+4)” and “p[4]” .– The only difference between the two is that we can change the value of “p” to any

integer variable address whereas “a” will always point to the integer array of length 10 defined.

Page 7: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Character Pointers, Arrays and Strings

• What is a String?– A string is a character array that is ‘\0’ terminated.– E.g. “Hello”

• What is a Character array?– It is an array of characters, not necessarily ‘\0’ terminated– E.g. char test[4] = {‘a’, ‘b’, ‘c’, ‘d’}; <this char array is not zero terminated>

• What is a character pointer?– It is a pointer to the address of a character variable.– E.g. char* a; <this pointer is not initialized>

Page 8: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Character Pointers, Arrays and Strings

• How do we initialize a Character pointer?– Initialize it to NULL. char* a = NULL;– Let it point to a character array.

• char* a; char b[100]; a = b;– Initialize to a character string.

• char* a = “Hello”; a pointer to the memory location where ‘H’ is stored. Here “a” can be viewed as a character array of size 6, the only difference being that a can be reassigned another memory location.

Page 9: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Examples• char* a = “Hello”;

– a -> gives address of ‘H’– *a -> gives ‘H’– a[0] -> gives ‘H’– a++ -> gives address of ‘e’– *a++ -> gives ‘e’– a = &b; where b is another char variable is perfectly LEGAL. However “char a[100];” “a

=&b;” where b is another char variable is ILLEGAL.

Page 10: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

first program in pointer

• // my first pointer • #include <iostream>• using namespace std;• int main ()• {• int a = 5, b = 15;• int *ptr;• ptr= &a;• *ptr = 10;• ptr = &b;• *ptr = 20;• cout<<"a="<<a<<" , b="<<b;• return 0;• }

Page 11: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

• #include <iostream>• using namespace std;• int main ()• {• int a = 5, b = 15;• int *p1, *p2;• p1 = &a;• p2 = &b;• *p1 = 10;• *p2 = *p1;• p1 = p2;• *p1 = 20;• cout << "a=" << a << " , b=" << b;• return 0;}

Page 12: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Array in pointer • #include <iostream>• using namespace std;• int main ()• {• int numbers[5];• int * p;• p = numbers; *p = 10;• p++; *p = 20;• p = &numbers[2]; *p = 30;• p = numbers + 3; *p = 40;• p = numbers; *(p+4) = 50;• for (int n=0; n<5; n++)• cout << numbers[n] << ", ";• return 0;• }

Page 13: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Value and address of pointer• Pointer type: int ,float double, char,long,short• Pointer name: *p,*AH• #include <iostream.h>• main()• {• int *ptr,i;• i=10;• ptr=&i;

• cout << "ptr = " <<*ptr<<endl;• //cout << "ptr = " <<*ptr<<"\t"<<&ptr<<endl;• system("pause");• }

Page 14: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

• #include <iostream.h>• main()• {• int* grades = NULL;• int numberOfGrades;

• cout << "Enter the number of grades: ";• cin >> numberOfGrades;• grades = new int[numberOfGrades];

• for (int i = 0; i < numberOfGrades; i++)• cin >> grades[i];

• for (int j = 0; j < numberOfGrades; j++)• cout << grades[j] << " ";

• delete [] grades;• grades = NULL;• system("pause");• }

Page 15: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Pointer in array • #include <iostream.h>• main()• {• int arr[2] ; • int *ptr;• arr[0]=10;• arr[1]=20;• ptr=&arr[0];

• cout << "ptr = " <<*ptr<<"\t"<<*++ptr<<endl;• system("pause");• }

Page 16: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Pointer in string

• #include <iostream.h>• main()• {• int arr[3] ; • char *ptr="GE 211";• cout << "ptr = " <<ptr<<endl;• system("pause");• }

Page 17: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

String

• #include <iostream.h>• main()• {• • char arr[300];

• cout << "enter string in = " ;• cin>>arr;• cout <<arr<<endl;• system("pause");• }

Page 18: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Pointer in array

• #include <iostream.h>• main()• {• • int arr[3]={100,200,300};• int *ptr,i;• ptr=arr;• for(i=0;i<3;i++)• cout <<i<<"\t"<<ptr[i]<<endl;• system("pause");• }

Page 19: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Pointer to another pointer • #include <iostream.h>

• main()• {• int p;• int *pt;• int **ptr;• p=10;• • cout << p << endl;• pt =&p;• ptr =&pt;• **ptr =5;• cout << p << endl;• system("pause");• }

Page 20: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Pointers in C++int number;int * p = &number;Its equvelant to int number;int * p;p = &number;

Page 21: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Return by value • #include<iostream.h>• int cubeByValue(int); // prototype• int main( )• {• int number = 5;• cout <<" The original value of number is " <<number<<endl;• number = cubeByValue(number);• cout << " The new value of number is " << number<< endl;• system("pause");• return 0;• }• int cubeByValue(int n)• {• return n*n*n; // cube local variable n• }

Page 22: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

call-by-reference with a pointer argument

• #include<iostream.h>• void cubeByReference (int *); // prototype• main( )• {• int number = 5;• cout<< " The original value of number is " << number • <<endl;• cubeByReference(&number);• cout<< " The new value of number is " << number <<endl;• system("pause");• return 0;• }• void cubeByReference (int *nPtr)• {• *nPtr = *nPtr * *nPtr * *nPtr; // cube number in main• }

Page 23: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Pointer in array

• int array1[3]={1,2,3};• for (int j=0;j<3;j++)• cout<<endl<<array1[j];• ****• int array1[3]={1,2,3};• for (int j=0;j<3;j++)• cout<<endl<< *(array1+j);

Page 24: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Pointer in string

• char str1[ ] =”with array";• char str2[ ] =”with pointer";• cout <<endl<<str1;• cout <<endl<<str2;• str2++;• cout <<endl<<str2;

Page 25: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

• #include<iostream.h>• main ( )• {• int a ; //a is an integer• int *aptr; // aptr is apointer to an integer• a = 7;• aptr = &a; // aptr set to address of a• cout <<" The address of a is " << &a <<endl• << "The value of aptr is " << aptr<< endl<< endl;

• cout << "The value of a is " << a<< endl• << "The value of *aptr is " << *aptr<< endl<<endl;• cout<<" Proving that * and & are complement of "• << "each other." <<endl<< " & *ptr = "<< & *aptr• << endl<< " *&aptr = " << *&aptr <<endl;• system("pause");• return 0;• }

Page 26: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

• call-by-value• call-by-reference • call by reference with pointer arguments.

Page 27: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Pointers and Arrays• The concept of array is very similar to the concept of pointer. The identifier of an

array actually a pointer that holds the address of the first element of the array.• Therefore if you have two declarations as follows:

– “int a[10];” “int* p;” then the assignment “p = a;” is perfectly valid– Also “*(a+4)” and “a[4]” are equivalent as are “*(p+4)” and “p[4]” .– The only difference between the two is that we can change the value of “p” to any

integer variable address whereas “a” will always point to the integer array of length 10 defined.

Page 28: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Character Pointers, Arrays and Strings

• What is a String?– A string is a character array that is ‘\0’ terminated.– E.g. “Hello”

• What is a Character array?– It is an array of characters, not necessarily ‘\0’ terminated– E.g. char test[4] = {‘a’, ‘b’, ‘c’, ‘d’}; <this char array is not zero terminated>

• What is a character pointer?– It is a pointer to the address of a character variable.– E.g. char* a; <this pointer is not initialized>

Page 29: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Adress in memory

• char * p = "hello";

Page 30: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

• char* cars[4]• char[0]="Mercedes";• char[1]="Nissan";• char[2]="Ferrari";• char[3]=“Toyota";

Page 31: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

• *p++; • important note that ++is priority than *• *p++ = *q++;• *p = *q;• p++;• q++;

Page 32: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Pointer to pointer

• char a;• char * b;• char ** c;• a = 'z';• b = &a;• c = &b;

Page 33: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Character Pointers, Arrays and Strings

• How do we initialize a Character pointer?– Initialize it to NULL. char* a = NULL;– Let it point to a character array.

• char* a; char b[100]; a = b;– Initialize to a character string.

• char* a = “Hello”; a pointer to the memory location where ‘H’ is stored. Here “a” can be viewed as a character array of size 6, the only difference being that a can be reassigned another memory location.

Page 34: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Examples• char* a = “Hello”;

– a -> gives address of ‘H’– *a -> gives ‘H’– a[0] -> gives ‘H’– a++ -> gives address of ‘e’– *a++ -> gives ‘e’– a = &b; where b is another char variable is perfectly LEGAL. However “char a[100];” “a

=&b;” where b is another char variable is ILLEGAL.

Page 35: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

• #include <iostream>• using namespace std;• int main ()• {• int a = 5, b = 15;• int *ptr;• ptr= &a;• *ptr = 10;• ptr = &b;• *ptr = 20;• cout<<"a="<<a<<" , b="<<b<<endl;• system("pause");• return 0;}

Page 36: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

• //DISPLAY 7.1 Program Using an Array• //Reads in 5 scores and shows how much each• //score differs from the highest score.• #include <iostream>

• int main( )• {• using namespace std;• int i, score[5], max;

• cout << "Enter 5 scores:\n";• cin >> score[0];• max = score[0];• for (i = 1; i < 5; i++)• {• cin >> score[i];• if (score[i] > max)• max = score[i];• //max is the largest of the values score[0],..., score[i].• }

• cout << "The highest score is " << max << endl• << "The scores and their\n"• << "differences from the highest are:\n";• for (i = 0; i < 5; i++)• cout << score[i] << " off by "• << (max - score[i]) << endl;• system("pause");• return 0;• }

Page 37: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

• #include <iostream>• using namespace std;• int main ()• {• int a = 5, b = 15;• int *p1, *p2;• p1 = &a;• p2 = &b;• *p1 = 10;• *p2 = *p1;• p1 = p2;• *p1 = 20;• cout << "a=" << a << " , b=" << b<< endl;• system("pause");• return 0;}

Page 38: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

• #include <iostream>• using namespace std;• int main ()• {• int numbers[5];• int * p;• p = numbers; *p = 10;• p++; *p = 20;• p = &numbers[2]; *p = 30;• p = numbers + 3; *p = 40;• p = numbers; *(p+4) = 50;• for (int n=0; n<5; n++)• cout << numbers[n] << ", ";• system("pause");• return 0;• }

Page 39: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

• In pointer & array • a[5] = 0;• *(p+5) = 0;

Page 40: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Dynamic Allocation

• int *pNumber;• pNumber = new int;• double *pDouble;• pDouble = new double;• #include <iostream.h>• int *pPointer;• void SomeFunction()• {• Pointer = new int;• *pPointer = 25;• }• main()• {• SomeFunction(); // pPointer • cout<<"Value of *pPointer: “<< *pPointer;• }

Page 41: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

• //DISPLAY 9.2 Arrays and Pointer Variables

• #include <iostream>• using namespace std;

• int main( )• {• int *p1, *p2;

• p1 = new int;• *p1 = 42;• p2 = p1;• cout << "*p1 == " << *p1 << endl;• cout << "*p2 == " << *p2 << endl;

• *p2 = 53;• cout << "*p1 == " << *p1 << endl;• cout << "*p2 == " << *p2 << endl;

• p1 = new int;• *p1 = 88;• cout << "*p1 == " << *p1 << endl;• cout << "*p2 == " << *p2 << endl;• cout << "Hope you got the point of this example!\n";• system("pause");• return 0;• }

Page 42: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

//DISPLAY 9.4 Arrays and Pointer Variables//Program to demonstrate that an array variable is a kind of pointer variable.#include <iostream>using namespace std;

typedef int* IntPtr;

int main( ){ IntPtr p; int a[10]; int index;

for (index = 0; index < 10; index++) a[index] = index;

p = a;

for (index = 0; index < 10; index++) cout << p[index] << " "; cout << endl;

for (index = 0; index < 10; index++) p[index] = p[index] + 1;

for (index = 0; index < 10; index++) cout << a[index] << " "; cout << endl; system("pause"); return 0;}

Page 43: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

//DISPLAY 9.6 A Two-Dimensional Dynamic Array #include <iostream>using namespace std;

typedef int* IntArrayPtr;

int main( ){ int d1, d2; cout << "Enter the row and column dimensions of the array:\n"; cin >> d1 >> d2;

IntArrayPtr *m = new IntArrayPtr[d1]; int i, j; for (i = 0; i < d1; i++) m[i] = new int[d2]; //m is now a d1 by d2 array.

cout << "Enter " << d1 << " rows of " << d2 << " integers each:\n"; for (i = 0; i < d1; i++) for (j = 0; j < d2; j++) cin >> m[i][j];

cout << "Echoing the two-dimensional array:\n"; for (i = 0; i < d1; i++) { for (j = 0; j < d2; j++) cout << m[i][j] << " "; cout << endl; } for (i = 0; i < d1; i++) delete[] m[i]; delete[] m;system("pause"); return 0;}

Page 44: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Pointer in array • #include <iostream.h>• main()• {• int Array[3];• Array[0] = 10;• Array[1] = 20;• Array[2] = 30;• int *pArray;• pArray = &Array[0];• cout<<"pArray points to the value "<<endl << *pArray<<endl;• system("pause");• }

Page 45: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

#include <iostream.h> main(){ int Array[3];Array[0] = 10;Array[1] = 20;Array[2] = 30;int *pArray;pArray = &Array[0];cout<<"pArray points to the value "<<endl << *pArray<<endl;pArray++;cout<<"pArray points to the value "<<endl << *pArray<<endl;pArray++;cout<<"pArray points to the value "<<endl << *pArray<<endl;system("pause");}

Page 46: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

• #include <iostream>// (int arg[]) =printarray• using namespace std;• void printarray(int arg[], int length) {• for (int n=0; n<length; n++)• cout << arg[n] << " ";• cout << "\n";• }• int main ()• {• int firstarray[] = {5, 10, 15};• int secondarray[] = {2, 4, 6, 8, 10};• printarray (firstarray,3);• printarray (secondarray,5);• system("pause");• return 0;• }

Page 47: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Adding content of array • #include <iostream>• using namespace std;• int main()• {• int A[]={5,6,1,9,12,4,7};• int sum=0;• for(int i=0;i<7;i++)• {• cout<<A[i]<<" ";• sum+=A[i]; //sum =sum+A[i];• }• cout<<"\nsum= "<<sum<<endl;• system("pause");• return 0;}

Page 48: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

• #include <iostream>// find char in array• using namespace std;• int main()• {• int arr[10],V,i;• bool found=false;• cout<<"enter 10 numbers please!\n";• for(i=0;i<10;i++)• {• cout<<"number"<<i+1<<"= ";• cin>>arr[i];• }• cout<<"enter V: ";• cin>>V;• for(i=0;i<10;i++)• if(arr[i]==V)• {• found=true;• break;• }• if(found) //same as if(found==true)• cout<<"V is in the array\n";• else• cout<<"V is not in the array\n";• system("pause");• return 0;}

Page 49: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

String in array• #include <iostream>• using namespace std;• #include <string>• int main ()• {• char MyName[20];• strcpy (MyName,"AHMED TELBA");• cout << MyName<<endl;• system("pause");• return 0;• }

Page 50: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

• //DISPLAY 7.1 Program Using an Array• //Reads in 5 scores and shows how much each• //score differs from the highest score.• #include <iostream>

• int main( )• {• using namespace std;• int i, score[5], max;

• cout << "Enter 5 scores:\n";• cin >> score[0];• max = score[0];• for (i = 1; i < 5; i++)• {• cin >> score[i];• if (score[i] > max)• max = score[i];• //max is the largest of the values score[0],..., score[i].• }

• cout << "The highest score is " << max << endl• << "The scores and their\n"• << "differences from the highest are:\n";• for (i = 0; i < 5; i++)• cout << score[i] << " off by "• << (max - score[i]) << endl;• system("pause");• return 0;• }

Page 51: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

//DISPLAY 7.3 Indexed Variable as an Argument//Illustrates the use of an indexed variable as an argument.//Adds 5 to each employee's allowed number of vacation days.#include <iostream>const int NUMBER_OF_EMPLOYEES = 3;int adjust_days(int old_days);//Returns old_days plus 5.int main( ){ using namespace std; int vacation[NUMBER_OF_EMPLOYEES], number; cout << "Enter allowed vacation days for employees 1" << " through " << NUMBER_OF_EMPLOYEES << ":\n"; for (number = 1; number <= NUMBER_OF_EMPLOYEES; number++) cin >> vacation[number-1]; for (number = 0; number < NUMBER_OF_EMPLOYEES; number++) vacation[number] = adjust_days(vacation[number]); cout << "The revised number of vacation days are:\n"; for (number = 1; number <= NUMBER_OF_EMPLOYEES; number++) cout << "Employee number " << number << " vacation days = " << vacation[number-1] << endl; system("pause"); return 0;}

int adjust_days(int old_days){ return (old_days + 5);}

Page 52: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

Dynamic Allocation• int *pNumber;• pNumber = new int;• double *pDouble;• pDouble = new double;• #include <iostream.h>• int *pPointer;• void SomeFunction()• {• Pointer = new int;• *pPointer = 25;• }• void main()• {• SomeFunction(); // pPointer • printf("Value of *pPointer: %d\n", *pPointer);• }

Page 53: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

• //DISPLAY 9.2 Arrays and Pointer Variables

• #include <iostream>• using namespace std;

• int main( )• {• int *p1, *p2;

• p1 = new int;• *p1 = 42;• p2 = p1;• cout << "*p1 == " << *p1 << endl;• cout << "*p2 == " << *p2 << endl;

• *p2 = 53;• cout << "*p1 == " << *p1 << endl;• cout << "*p2 == " << *p2 << endl;

• p1 = new int;• *p1 = 88;• cout << "*p1 == " << *p1 << endl;• cout << "*p2 == " << *p2 << endl;• cout << "Hope you got the point of this example!\n";• system("pause");• return 0;• }

Page 54: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

//DISPLAY 9.4 Arrays and Pointer Variables//Program to demonstrate that an array variable is a kind of pointer variable.#include <iostream>using namespace std;

typedef int* IntPtr;

int main( ){ IntPtr p; int a[10]; int index;

for (index = 0; index < 10; index++) a[index] = index;

p = a;

for (index = 0; index < 10; index++) cout << p[index] << " "; cout << endl;

for (index = 0; index < 10; index++) p[index] = p[index] + 1;

for (index = 0; index < 10; index++) cout << a[index] << " "; cout << endl; system("pause"); return 0;}

Page 55: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

• #include <cstdlib>• #include <cstddef>• typedef int* IntArrayPtr;• void fill_array(int a[], int size);• //Precondition: size is the size of the array a.• //Postcondition: a[0] through a[size-1] have been• //filled with values read from the keyboard.• void sort(int a[], int size);• //Precondition: size is the size of the array a.• //The array elements a[0] through a[size-1] have values.• //Postcondition: The values of a[0] through a[size-1] have been rearranged• //so that a[0] <= a[1] <= ... <= a[size-1].• int main( )• { using namespace std;• cout << "This program sorts numbers from lowest to highest.\n";• int array_size;• cout << "How many numbers will be sorted? ";• cin >> array_size;• IntArrayPtr a;• a = new int[array_size];• fill_array(a, array_size);• sort(a, array_size);• cout << "In sorted order the numbers are:\n";• for (int index = 0; index < array_size; index++)• cout << a[index] << " ";• cout << endl;• delete [] a;• system("pause");• return 0;}• //Uses the library iostream:• void fill_array(int a[], int size)• { using namespace std;• cout << "Enter " << size << " integers.\n";• for (int index = 0; index < size; index++)• cin >> a[index];}• sort(int a[], int size)• //Any implementation of sort may be used. This may or may not require some • //additional function definitions. The implementation need not even know that • //sort will be called with a dynamic array. For example, you can use the • //implementation in Display 7.12 (with suitable adjustments to parameter names).

Page 56: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

//DISPLAY 9.6 A Two-Dimensional Dynamic Array #include <iostream>using namespace std;

typedef int* IntArrayPtr;

int main( ){ int d1, d2; cout << "Enter the row and column dimensions of the array:\n"; cin >> d1 >> d2;

IntArrayPtr *m = new IntArrayPtr[d1]; int i, j; for (i = 0; i < d1; i++) m[i] = new int[d2]; //m is now a d1 by d2 array.

cout << "Enter " << d1 << " rows of " << d2 << " integers each:\n"; for (i = 0; i < d1; i++) for (j = 0; j < d2; j++) cin >> m[i][j];

cout << "Echoing the two-dimensional array:\n"; for (i = 0; i < d1; i++) { for (j = 0; j < d2; j++) cout << m[i][j] << " "; cout << endl; } for (i = 0; i < d1; i++) delete[] m[i]; delete[] m;system("pause"); return 0;}

Page 57: Pointer in C++ Dr. Ahmed Telba. Topics Covered Introduction to Pointers Pointers and arrays Character Pointers, Arrays and Strings Examples

• #include <iostream.h>• int mult( int a, int b)• {• return (a*b);• }

• main( )• {• int x, y, z;• cin >> x >> y >> z;• cout << "x = " << x << " y = " << y << " z = " << z << endl;• cout << "product1 ="<< mult (x ,y) << endl;• cout << "product2 " << mult (x +2, y) << endl;• system("pause");• return 0;• }