Pointers: These Are The Variables That Are Used To Hold The Address of Another Variable
Pointers: These Are The Variables That Are Used To Hold The Address of Another Variable
ADDR16 Contents16
Lect 14 P. 6
Pointer Variable
Assume ptr is a pointer variable and x is an integer variable
x 10
ptr &x
// variable address= p
// variable value=*p
Another Program
int a=20;
int *p;
p=&a;
Cout<<a<<&a;
Cout<<p<<&p<<*p;
P has an address 500
Variables, Addresses and Pointers
9-13
• int main()
• { float a[5];
• float *ptr;
• cout << "Displaying address using arrays: "<<endl;
• for (int i = 0; i < 5; ++i)
• { cout << &a[i] <<endl; }
• ptr = a; // ptr = &a[0]
• cout<<"\nDisplaying address using pointers: "<< endl;
• for (int i = 0; i < 5; ++i)
• { cout << ptr+i <<endl; } return 0; }
Display addresses using pointer
notation
• int main()
• { float a[5];
• cout<<"Displaying address using pointers notation:
"<< endl;
• for (int i = 0; i < 5; ++i)
• { cout << a+i <<endl; }
• return 0; }
• int p[5] = {3, 4, 5, 5, 3};
• &p[0] is equal to p and *p is equal to p[0]
• &p[1] is equal to p+1 and *(p+1) is equal to p[1]
• &p[2] is equal to p+2 and *(p+2) is equal to p[2]
• &p[i] is equal to p+i and *(p+i) is equal to p[i]
Assignment in pointers
void main()
{
int a=25,b=78,sum;
int *x,*y;
x=&a;
y=&b;
sum= *x + *y;
cout<<“Sum is : ”<<sum;
}
Pointer Assignment
Pointer Arithmetic
Void main(){
Int *p,a,**p1;
Clrscr();
Cout<<“Enter the value of a”;
Cin>>a;
p=&a;
p1=&p;
Cout<<“address of variable a using ptr”<<p;
p=p+4;
Cout<<“modified address is”<<p;
Cout<<“value of variable a using ptr is”<<*p;
*p=*p+20;
Cout<<“modified value of variable a is ”<<*p;
Cout<<“address of pointer p is”<<p1;
getch();}
Void Pointer
Example:
main()
{ int i;
char c;
void *data;
i = 6;
c = 'a';
data = &i;
data = &c;
getch();
}
Other Properties of Void Pointer
• int* ptr;
• ptr=0;
• The above statement denotes exforsys as an integer
pointer type that does not point to a valid memory
address. This shows that exforsys has a NULL pointer
value.
Difference between Null Pointer and Void
Pointer
A Void pointer is a special type of pointer of void and
denotes that it can point to any data type.
NULL pointers can take any pointer type, It means
pointer can’t point to anything .It resolves the
problem of dangling and wild pointers.
Dangling Pointers
Ex: class A
{
private:
int m;
public:
void show();
};
• Define a pointer to the member m as follows:
int A ::* ip = &A :: m;
– ip pointer created thus acts like a class member in
that it must be invoked with a class object.
– A::* means “pointer-to-member” of A class.
– &A::m means the “address of the m member of A
class”.
– ip now be used to access the member m inside
member functions.
• The dereferencing operator ->* is used to access a
member when we use pointers to both the object
and the member.
• The dereferencing operator .* is used when object
itself is used with the member pointer.
Example: pointer to data member and
member function
#include <iostream> // create an object of class type X
using namespace std;
X xobject;
class X
{ // initialize data member
public: xobject.*ptiptr = 10;
int a;
void f(int b) cout << "The value of a is " << xobject.*ptiptr<<
{
endl;
cout << "The value of b is "<< b
<< endl;
} // call member function
}; (xobject.*ptfptr) (20);
int main() }
{
// declare pointer to data member
int X::*ptiptr = &X::a;
//cout<<"price"<<(*this).price<<end
l;