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

Slides:



Advertisements
Similar presentations
CSC 270 – Survey of Programming Languages C Lecture 6 – Pointers and Dynamic Arrays Modified from Dr. Siegfried.
Advertisements

Pointers in C Rohit Khokher
Kernighan/Ritchie: Kelley/Pohl:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Arrays.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Pointers Pointer a data type stores a memory address points to whatever the memory location contains A pointer is a variable that can store a memory address.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
C/C++ Basics (IV) Array, Pointers and Dynamic Arrays Berlin Chen 2003 Textbook: 1. Walter Savitch, “Absolute C++,” Addison Wesley, 2002 開發代理 2. Walter.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Pointers and Dynamic Memory Allocation. Declaring a pointer.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
CS102 Introduction to Computer Programming Chapter 9 Pointers.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Array & Matrix Selected topics. 1. It will compare two adjacent elements, if second element is smaller than the first then it will swap them, if we wanted.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
POINTERS.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
Variables and memory addresses
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Pointers and Dynamic Memory Allocation. Declaring a pointer.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
ARRAYS (C) KHAERONI, M.SI. OVERVIEW Introduction to Arrays Arrays in Functions Programming with Arrays Multidimensional Arrays.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Student Book An Introduction
CSC 270 – Survey of Programming Languages
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Arrays Lecture 11.
Arrays Arrays A few types Structures of related data items
CISC181 Introduction to Computer Science Dr
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Pointers.
Presentation transcript:

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.

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

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.

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.

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’}; What is a character pointer? – It is a pointer to the address of a character variable. – E.g. char* a;

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.

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.

first program in pointer // my first pointer #include 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; }

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

Array in pointer #include 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; }

Value and address of pointer Pointer type: int,float double, char,long,short Pointer name: *p,*AH #include main() { int *ptr,i; i=10; ptr=&i; cout << "ptr = " <<*ptr<<endl; //cout << "ptr = " <<*ptr<<"\t"<<&ptr<<endl; system("pause"); }

#include 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"); }

Pointer in array #include main() { int arr[2] ; int *ptr; arr[0]=10; arr[1]=20; ptr=&arr[0]; cout << "ptr = " <<*ptr<<"\t"<<*++ptr<<endl; system("pause"); }

Pointer in string #include main() { int arr[3] ; char *ptr="GE 211"; cout << "ptr = " <<ptr<<endl; system("pause"); }

String #include main() { char arr[300]; cout << "enter string in = " ; cin>>arr; cout <<arr<<endl; system("pause"); }

Pointer in array #include 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"); }

Pointer to another pointer #include main() { int p; int *pt; int **ptr; p=10; cout << p << endl; pt =&p; ptr =&pt; **ptr =5; cout << p << endl; system("pause"); }

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

Return by value #include 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 }

call-by-reference with a pointer argument #include 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 }

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

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

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

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

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.

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’}; What is a character pointer? – It is a pointer to the address of a character variable. – E.g. char* a;

Adress in memory char * p = "hello";

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

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

Pointer to pointer char a; char * b; char ** c; a = 'z'; b = &a; c = &b;

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.

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.

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

//DISPLAY 7.1 Program Using an Array //Reads in 5 scores and shows how much each //score differs from the highest score. #include 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; }

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

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

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

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

//DISPLAY 9.2 Arrays and Pointer Variables #include 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; }

//DISPLAY 9.4 Arrays and Pointer Variables //Program to demonstrate that an array variable is a kind of pointer variable. #include 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; }

//DISPLAY 9.6 A Two-Dimensional Dynamic Array #include 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; }

Pointer in array #include 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"); }

#include 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"); }

#include // (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; }

Adding content of array #include 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;}

#include // 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;}

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

//DISPLAY 7.1 Program Using an Array //Reads in 5 scores and shows how much each //score differs from the highest score. #include 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; }

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

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

//DISPLAY 9.2 Arrays and Pointer Variables #include 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; }

//DISPLAY 9.4 Arrays and Pointer Variables //Program to demonstrate that an array variable is a kind of pointer variable. #include 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; }

#include 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).

//DISPLAY 9.6 A Two-Dimensional Dynamic Array #include 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; }

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