Professional Documents
Culture Documents
Defn
<data type>*<ptr-name>;
Examples
(only pseudo code-for concept)
1.
int a=10;
int b;
b=a;
b=&a; wrong----
variable doesnt hold address,so we need special
variable to hold address of variable called
pointer.
So Weve-----
int *b;
b=&a;
2.
Int a=10;
Int *p; p is a pointer which points an integer type
variable or,p is
pointer to an integer. * is indirection operator
P=&a; p is pointing as value. Or, p stores address
of a
or,value stored at p=address of a.
& is address operator.
Note-----
a is direct addressing
*p is indirect addressing(*p=value at address stored at p)
10
a
100 p
0
*p
* Is polymorphic in c
i>multiplication operator
ii>indirection operator
*p means dereferencing pointer
P=&a means p referenced to a
3.
int a=10;
char *p;
p=&a;wrong
4.
Int a=10;
Char *P;
P=(char*)&a;
Printf(%d,&p);
5.
Only address stored in pointer ,not values
Int a=10;
Int *p;
6.
Int a=10;
Int *p;
*p=a;(wrongsometm run time error)
1 605
0 8
a p
6058 overwritten by 10(as value)
{if 6058 is free,otherwise code crash}
When pointer is dereferenced without any address as
above
ie. Int *p=a
it is called wild pointer,any there would be chance of
crashing code,so must supply some address
ie. Int *p=some address
or,
int *p=null;
or,int *p=\0;
so, to avoid run time error in above code,weve-----
7.
Int a=10;
Int *p=null;(0th address in memory is reserved in o/s as
null)
*p=a;//dereferencing
Note-although this is not 100% correct but safe almost.
So,dereference only when addressed while declaring..
8.
int *p;
p=(int *)7000; //a no. is made like address by
typecasting
but problem may occur if address 7000 is engaged
somewhere
becoz, by default a value cant be address.
9.
Int *p;
P=7000;
10.
Int *p;
P=&7000;
11.
Pointer never points itself
Int *p;
P=&p;(wrong)
12.
Int a=10;
Int *p;
Int *q;
p=&a;
q=&p;(wrong)
Int a=10;
Int *p;
p=&a;
q=&p;(true now)
note-
pointer is backbone of c.