Beruflich Dokumente
Kultur Dokumente
5 Pointers in C++
Pointer
0
1
2
3
.
.
.
.
.
.
.
65,535
Memory Organization
Pointer
Quantity Variable
126 Value
5000 Address
Representation of a Variable
5048
P 5000
Pointer Variable
Pointer
Pointer
data_type *ptr_name;
Example:
int *p; /*integer pointer */
float *x; /* float pointer */
P ? ?
Contains Points to
Garbage Unknown location
Multiple Declarations:
Example:
int quantity;
int *p; /*Declaration*/
p = &quantity; /*Initialization*/
or
int *p = &quantity;
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
.........
.........
* - Indirection operator or
Dereferencing operator.
Example:
n = *&marks;
Chain of Pointers
p2 p1 Variable
Multiple Indirections
int main( )
{
int x, *p1, **p2;
x = 100;
p1 = &x;
p2 = &p1;
cout<<“Value :”<<**p2;
return (0);
}
Pointer Expressions
y = *p1 * *p2;
sum = sum + *p3;
z = 5 - *p2/ *p1;
*p2 = *p1 + 10;
p1++;
-p2;
sum += *p2;
Elements
X[0] X[1] X[2] X[3] X[4]
Value 8 4 9 6 3
1000 1002 1004 1006 1008
Address
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);
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;
}
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;
}
Pointer to Function
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;
}
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;
}
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;
}
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;
}
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);
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);