Type Checking In C++ Pointer
In
c++ different type of pointer and some are already like pointer they show there
behavior according to datatype.
If
want to check type checking then we have to use 2 types pointer.
1.Void
2.Null
1.Void
A void pointer is a general-purpose pointer that can hold the address of any data type, but it is not associated with any data type. we cannot assign the address of a variable to the variable of a different data type.
Fig: Void Pointer |
Example:
void *p;
Program:
#inlcude<iostream.h>
#include<conio.h>
void main()
{
int a;
void *p;
int b;
cout<<”example of
void datatype/n”;
p=&a;
cout<<*(int *)a;
p=&b;
cout<<*(int *)b;
getch();
}
Output:
Example of void
datatype
0767
2.Null
Just like normal variables, pointers are not initialized when they
are instantiated. Unless a value is assigned, a pointer will point to some
garbage address by default.
Besides memory addresses, there is one additional value that a
pointer can hold: a null value. A null value is a special value
that means the pointer is not pointing at anything. A pointer holding a null
value is called a null pointer.
Fig: NULL Pointer
Fig: NULL Pointer
Example:
int *p=null;
Program:
#inlcude<iostream.h>
#include<conio.h>
void main()
{
int a;
int *p=null;
cout<<”input one number”;
cin>>a;
p=&a;
cout<<*p;
getch();
}
Output:
Input one no 23
23
No comments:
Post a Comment