Chapter no. 5 Pointers in C++
Pointer Memory Cell
Address 0 1 2 3 . . . . . . . 65,535
Memory Organization
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
-1-
Chapter no. 5 Pointers in C++
Pointer int quantity = 126; Quantity
Variable Value
126 5000
Address
Representation of a Variable
Variable
Value
Quantity
126
P
5000
Address 5000
5048
Pointer Variable
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
-2-
Chapter no. 5 Pointers in C++
Pointer
Pointer Constants
Pointer Values
Pointer Variables
Pointer
Pointer Constants: Memory addresses within a computer. Pointer Value: Value stored at the address. Pointer Variable: Pointer value can be stored in another variable.
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
-3-
Chapter no. 5 Pointers in C++
Accessing address of the variable & -
‘Address of’ operator.
This unary operator is used to obtain address of a variable and can only be used with single variable or single array element. It can not be used with constants, whole array or expressions.
Declaring pointer variables data_type
*ptr_name;
This tells compiler three things about the variable ptr_name: 1. 2. 3.
* tells that the variable ptr_name is a pointer variable. ptr_name needs a memory location. ptr_name points to a variable of type data_type.
Example: int *p; float *x;
/*integer pointer */ /* float pointer */
P
?
Contains Garbage
?
Points to Unknown location
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
-4-
Chapter no. 5 Pointers in C++
Pointer Declaration Style int* p; int *p; int * p;
/* Style 1*/ /* Style 2*/ /* Style 3*/
Multiple Declarations: int *p, x, *q; Initialization of Pointer Variables Example: int quantity; int *p; p = &quantity;
/*Declaration*/ /*Initialization*/
or int *p = &quantity;
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
-5-
Chapter no. 5 Pointers in C++
Pointers Flexibility int x, y, z, *p; ...... p = &x; ...... p = &y; ...... p = &z; ......
int x;
x
y
z
p
p1
p2
p3
int *p1 = &x; int *p2 = &x; int *p3 = &x;
x
......... .........
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
-6-
Chapter no. 5 Pointers in C++
Accessing variable through pointer
* -
Indirection operator or Dereferencing operator.
We can call it ‘Contents of’. Example: int marks, *p, n; marks = 79; p = &marks; n = *p; ------------------------------p = &marks; n = *p; Equivalent to: n = *&marks; By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
-7-
Chapter no. 5 Pointers in C++
Chain of Pointers Address2
Address1
p2
p1
Value
Variable
Multiple Indirections int main( ) { int x, *p1, **p2; x = 100; p1 = &x; p2 = &p1; cout<<“Value :”<<**p2; return (0); } By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
-8-
Chapter no. 5 Pointers in C++
Pointer Expressions y = *p1 * *p2; sum = sum + *p3; z = 5 - *p2/ *p1; *p2 = *p1 + 10; p1++; -p2; sum += *p2; We may not use pointers in Division / Multiplication
i.e. p1 / p2 or p1 * p2 or p1 / 3 Similarly, two pointers can not be added. i.e. p1 + p2 is not allowed.
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
-9-
Chapter no. 5 Pointers in C++
Pointers and Arrays int x[5] = {8, 4, 9, 6, 3}; Elements
Value Address
X[0]
X[1]
X[2]
X[3]
8
4
9
6
3
1000
1002
1004
1006
1008
X[4]
Assigning the array to pointer: int *p; p = x;
or
p = &x[0];
Now, we can access every value of x using p++ to move from one element to another.
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
- 10 -
Chapter no. 5 Pointers in C++
Pointers and Arrays int main( ) { int *p, i; int array[5] = {8, 9, 6, 3, 2}; p = array; for(i=0; i<5; i++) { cout<<“%d ”<<*p; //1 p++;
//2
} return 0; } 1
cout<<“\n %d”<<*(p+i);
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
- 11 -
Chapter no. 5 Pointers in C++
Call by value void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } int main( ) { int x, y; x = 10, y = 14; swap(x,y); cout<<"\nX ="<<x; cout<<"\nY ="<
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
- 12 -
Chapter no. 5 Pointers in C++
Call by reference void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } int main( ) { int x, y; x = 10, y = 14; swap(&x,&y); cout<<"\nX ="<<x; cout<<"\nY ="<
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
- 13 -
Chapter no. 5 Pointers in C++
Pointer to Function
void display(int *n, int total) { int i = 0; cout<<"The array is: \n"; while(i < total) { cout<<endl<<*n; n++; i++; } } int main( ) { int array[ ] = {8, 4, 6, 2, 5}; display(array,5); return 0; }
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
- 14 -
Chapter no. 5 Pointers in C++
Pointer to Function
int largest(int *n, int total) { int i = 0,max=*n; while(i
max) max = *n; n++; i++; } return max; } int main( ) { clrscr( ); int array[ ] = {5,6,9,1,2}; int max = largest(array,5); cout<<"\nLargest number:"<<max; return 0; }
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
- 15 -
Chapter no. 5 Pointers in C++
Pointer to Function
int *largest(int *n, int total) { int i = 0, *max = n; while(i < total) { if(*n > *max) *max = *n; n++; i++; } return max; } int main( ) { clrscr( ); int array[ ] = {45,66,19,81,32}; int *max; max = largest(array,5); cout<<"\nMax number:"<<*max; return 0; }
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
- 16 -
Chapter no. 5 Pointers in C++
Pointer to String
int main( ) { clrscr( ); char *s; int len = 0; cout<<"Enter the string: "; cin>>s; while(*s) // *s!='\0' { len++; s++; } cout<<"\nLength: "<
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
- 17 -
Chapter no. 5 Pointers in C++
Pointer to object class Menu { int code; float price; public: void input(int a, float b) { code = a; price = b; } void show( ) { cout<<"\nCode : "<input(45, 85.20); //m.input( ) z->show( ); //(*z).show( ) return 0; } By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
- 18 -
Chapter no. 5 Pointers in C++
Pointer to array of objects class Maths { int n; public: void accept( ) { cout<<"\nEnter number:"; cin>>n; } int cube( ) { int c = n*n*n; return c; } }; int main( ) { clrscr( ); int val; Maths *p = new Maths[5]; Maths *t = p; for(int i=0; i<5; i++, p++) p->accept( ); for(i=0; i<5; i++, t++) { val = t->cube( ); cout<<"\nCube: "<
- 19 -
Chapter no. 5 Pointers in C++
Pointer to function double square(double a) { return (a*a); } double cube(double b) { return (b*b*b); } double findValue(double x, double (*fun)(double)) { double z = (*fun)(x); return z; } int main( ) { double d = 12.3; double (*p) (double); cout<<"\n\nUsing first function:"; p = sqrt; cout<<"\nResult:"<
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
- 20 -
Chapter no. 5 Pointers in C++
Pointer to function
void check(char *a, char *b, char*(*str) (char *, const char *)) { (*str)(a,b); cout<
By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09
- 21 -