Pointers Cpp
Pointers Cpp
• int *x;
• float *f;
• char *c;
• In the first declaration x is an integer pointer it tells the
compiler that it holds address of any integer variable.
• Similarly float pointer and character pointer.
• The indirection operator (*) also called deference
operator
• When a pointer is dereference, the value at that
address stored by the pointer is retrieved.
• The indirection operator is used for two distinct
purpose with pointer declaration and deference.
Declaring a Pointer Variable
ADDR16 Contents16
Lect 14 P. 10
Pointer Variable
Assume ptr is a pointer variable and x is an integer variable
x 10
ptr &x
int main()
{
int x;
int *ptr;
x = 10;
ptr = &x;
*ptr = *ptr + 1;
cout<< x;
return 0;
}
Constant Pointers
25
CIS 15 Pointer
Arithmetic
Constant Pointer
Constant pointer means the pointer is constant.
Constant pointer is NOT pointer to constant.
For eg:
int * const ptr2 indicates that ptr2 is a pointer
which is constant. This means that ptr2 cannot be
made to point to another integer.
However the integer pointed by ptr2 can be
changed.
//const pointer void
main()
{
int i = 100,k;
int* const pi = &i;
*pi = 200;
pi=&k; //won't compile
}
Constant Pointers to Constants
Example:
int value = 22;
const int * const ptr = &value;
28
CIS 15 Pointer
Arithmetic
//const pointer to a const void
f3()
{
int i = 100;
const int* const pi = &i;
//*pi = 200; <- won't compile
//pi++; <- won't compile
}
Pointer to constant
Pointer to constant is
const int * ptr1 indicates that ptr1 is a pointer that
points to a constant integer. The integer is constant
and cannot be changed. However, the pointer ptr1
can be made to point to some other integer.
• //pointer to a const
void f1()
{
int i = 100;
const int* pi = &i;
//*pi = 200; <- won't compile
pi++;
}
Pointer arithmetic
37
CIS 15 Pointer
Arithmetic
Void pointers
• When a variable is declared as being a pointer to
type void it is known as a generic pointer.
• Since you cannot have a variable of type void, the
pointer will not point to any data and therefore
cannot be dereferenced.
• It is still a pointer though, to use it you just have to
typecast it to another kind of pointer first. Hence
the term Generic pointer.
• This is very useful when you want a pointer to point
to data of different types at different times.
• Syntax:
void * variable name;
*(data_type*)name of variable;
void main()
{ int i;
char c;
void *data;
i = 6;
c = 'a';
data = &i;
cout<<"the_data points to the integer value “<< *(int*) data;
data = &c;
cout<<"the_data now points to the character “<< *(char*) data;
}
Null Pointer
p = numbers;
After that, p and numbers would be equivalent and would
have the same properties. The only difference is that
we could change the value of pointer p by another one,
whereas numbers will always point to the first of the 20
elements of type int with which it was defined.
Therefore, unlike p, which is an ordinary pointer,
numbers is an array, and an array can be considered a
constant pointer.
#include <iostream.h>
main ()
{ int numbers[5];
int * p;
p = numbers;
*p = 10;
p++;
*p = 20;
p = &numbers[2];
Cont…
…cont.
*p = 30;
p = numbers + 3;
*p = 40; p = numbers;
*(p+4) = 50;
for (int n=0; n<5; n++)
cout<<numbers[n];
}
In arrays we used brackets ([]) several times in
order to specify the index of an element of the
array to which we wanted to refer. Well, these
bracket sign operators [] are also a dereference
operator known as offset operator. They
dereference the variable they follow just as *
does, but they also add the number between
brackets to the address being dereferenced
For example:
a[5] = 0; // a [offset of 5] = 0
*(a+5) = 0; // pointed by (a+5) = 0
str2=str1; /error/
q= s; /works/
return;
}
Array of pointers
• It is nothing but a collection of address.
Void main()
{
int a[5]={1,2,3,4,5},i;
int *p[5];
for(i=0;i<5;i++)
{
p[i]=a+i;
}
for(i=0;i<5;i++)
{
cout<<*(p+i)<<“value”;
cout<<“at location ”<<p+I;
}
}
Pointers to pointers
• A pointer variable containing address of another
pointer variable is called pointer to pointer
void main()
{
int a=2, *p, **q;
p=&a;
q=&p;
cout<<a<<“is stored at ”<<p<<“and pointer is
stored at ”<<q;}
Difference between *p[3] and (*p)[3]
Class base1
{
Public:
Void display()
{
Cout<<“base class displayed”;
}
};
Class der1:public base1
{
Public:
Void display()
{cout<<“derived class displayed”;}
};
Void main()
{
base1 *ptr;
der1 d;
ptr=&d;
ptrdisplay();
getch()
}
Output
Base class displayed
Virtual function