Pointer
Pointer is nothing but a simple game of the address of memory with two letter, “&” and “*”.
Actually pointer is a variable that holds memory address in. Whose value refers another value, stored
elsewhere in the memory using the address.
“&” indicates the address of the data and “*” indicates the value of the data.
int roll;
scanf(“%d”,&roll);
In the upper code “&roll” means that the value given by the user stored in the location of roll in
primary memory. The value of address is always in hex format (like 64589).
Declaring the pointer:
data_type *ptr_name
exp:
int *ptr_name , float *ptr_name , char *ptr_name;
printf(“The address is %u”,ptr_name);
int *p;
int r = 5;
p = &r; // p holds the address of data r
printf(“%d stored in the address location %u”,r,p);
printf(“%d is stored”,*p);
p is a pointer type integer & r is a integer type data member. “&r” means the address of r,and its
stored in p. When you are print the address you have to denote it like “%u”,as we have done in this
code. It will print “5 stored in the address location 65385”. And in the next statement will print “5 is
stored”. As we told before that * is the symbol denotes data,not the address,so it will print the data in
the address location p.
Pointer Conversions:
One type of pointer can be converted into another type of pointer. There are two way to do this
those that involve void* pointer,and those that don't.
In C,its permissible to assign a void* pointer to any other ype of pointer. Its also permissible to
assign other type of pointer into void* pointer. The void* pointer is used to specify a pointer who's base
type is void. The void type allows a function to specify a parameter that is capable to receive any type
of pointer argument without a reporting a type mismatch.
The other way is explicit cast . However the conversion of pointer may create a undefined
behavior.
Exp: double x = 100.001,y;
int *p;p = (int *) &x; // it causes p to point a double being a int pointer
y = *p; // the attempt to assign y the value x through p
this may not operate properly.
Pointer Arithmetic:
You can do any arithmetic operation with the pointer values.
P++;
It means the next address of the location p. If p was 2001 then after execute this it will be 2002.
p--;
Now it will decrease the value of the address & make the previous address.
*[ p + 1 ];
This denotes the value of the data stored in the address (p+1).
[ *p + 1 ];
[*p+1 ] means the value of data stored in address p with 1. Let say 5 is stored in position p. The
result will be 5 + 1 = 6.
And you can do p1 = p1 + p2; This just add th location of p1 & p2 ,And store in p1.
Array of pointer::
int *pld[10];
pld[10] is an array, and it is a pointer of integer type. Its an array of pointer,so it is array of
pointer.
Exp:
int t;
for(t=0;t<10;t++)
printf(“%d”, *q[t]);
Pointer to an array:
int (*pld)[10];
*pld is a pointer variable like any other variable. And we take its array like n[10]. Here n is
(*pld).

No comments:
Post a Comment