Pointer are the variable that store addresses of other
variable as their value.
e.g we declare an ‘int’ type variable and initialize it
int x = 12 ;
In memory this ‘x’ must have some addresses where it is stored,
that addresses is a stored in pointer variable.
Q ) How pointers are declared ?
A ) Declaration of pointer variable is almost the same as
that of other variable, only one change is that we add ‘ * ’ operator before variable
name .The type of pointer variable will be same as that of variable whose
address is stored in pointer .i.e, if you want to store address of ’ int ‘ type
variable than the pointer should also be of ‘ int ‘ type .
Int *ptr; // declaration of pointer
Q ) How to initialize a pointer ?
A ) As pointer store address only so we cannot initialize it
using any number this will create a problem in runtime not compile time, that
mean’s it run the program but create problem during execution so we always initialize
by giving addresses of some variable using the addresses(&) operator .
Ptr =
&x ;
This means
that pointer ptr store address of x or ptr is pointing toward x.
Don’t
write *ptr during initialize .i will tell why not to write later in this
article.
Q ) Working of pointer ?
A ) Different working of pointer , will be discussed one by
one.
1)
We can
use pointer to print the address of any variable .
2)
We can print the value of variable whose address
is stored in pointer
3)
We can print the address of pointer
This is the program and I use
this program to discuss next point, so firstly see this program carefully
You must understand the program
as we discuss these thing above
#include<stdio.h>
int main()
{
int
a=5;
int *ptr;
ptr = &a;
}
à
Print the value using pointer
The value of a can be print using
pointer by simply adding ‘*’
e.g ; printf(“THE VALUE OF A %d “
, *ptr);
this
will print 5 , so value of ‘a’ can be print by two method , directly or by
pointer
As ‘ptr’ is pointing the ‘a’ , so
value can be printed using ‘*’.
à
print the address of variable using pointer
The
address can be simply print by writing ptr because the ptr contain address of ‘a’
as it value
e.g;
printf(‘’value of ptr %x’’,ptr); //for address we use the %x
this
will print the address of ‘a’
àPrint
the address of pointer
The
address of pointer can be printed using & operator with the pointer
e.g. printf (“address of ptr %x”
, &ptr );
this will print the pointer’s
address
These are the general concept of
pointer’s we can use these pointer for different manipulation of program like
we can change address because we can access .
Most of the high level language
do not support pointer like java. In java pointer are eliminated.
This is the one program for pointer . I will post more
program related to pointer later on
Try to solve it’s output
#include<stdio.h>
int main()
{
int a=5;
int *aptr;
aptr = &a;
printf("\nvalue of aptr
= %p",aptr);
printf("\naddress of a
= %p",&a);
printf("\naddress of aptr
= %p",&aptr);
printf("\nvalue of a =
%d",a);
printf("\n*aptr point to a
= %d",*aptr);
printf("\n&*aptr = %p",&*aptr);
return 0;
}
No comments:
Post a Comment