Sie sind auf Seite 1von 21

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

126 Value

5000 Address

Representation of a Variable

Variable Value Address

Quantity 126 5000

5048
P 5000

Pointer Variable

By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09 -2-


Chapter no. 5 Pointers in C++

Pointer

Pointer Pointer Pointer


Constants Values 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. * tells that the variable ptr_name is a pointer


variable.
2. ptr_name needs a memory location.
3. ptr_name points to a variable of type
data_type.

Example:
int *p; /*integer pointer */
float *x; /* float pointer */

P ? ?
Contains Points to
Garbage 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; /* Style 1*/


int *p; /* Style 2*/
int * p; /* Style 3*/

Multiple Declarations:

int *p, x, *q;

Initialization of Pointer Variables

Example:

int quantity;
int *p; /*Declaration*/
p = &quantity; /*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;
...... x y z
p = &x;
......
p = &y;
......
p = &z;
...... p

int x; 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 Value

p2 p1 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
X[0] X[1] X[2] X[3] X[4]

Value 8 4 9 6 3
1000 1002 1004 1006 1008
Address

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 ="<<y;
return 0;
}

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 ="<<y;
return 0;
}

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<total)
{
if(*n>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: "<<len;
return 0;
}

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 : "<<code;
cout<<"\nPrice: "<<price;
}
};
int main( )
{
clrscr( );
Menu m, *z;
z = &m;
z->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: "<<val;
}
return 0;
}

By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09 - 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:"<<findValue(d, p);

cout<<"\n\nUsing second function:";


p = square;
cout<<"\nResult:"<<findValue(d, p);

cout<<"\n\nUsing second function:";


p = cube;
cout<<"\nResult:"<<findValue(d, p);
return 0;
}

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<<a;
}
int main( )
{
char *ch = "abc";
char *dh = "xyz";
clrscr( );
char *(*p) (char *, const char *);
cout<<"\nUsing first function:";
p = strcpy;
check(ch,dh,p);

cout<<"\nUsing second function:";


p = strcat;
check(ch,dh,p);
return 0;
}

By, Kute T. B. for Object Oriented Programming (SYIF) 2008-09 - 21 -

Das könnte Ihnen auch gefallen